does anyone have any sample code they’ve used to handle a scenario like this?
i have a checkbox to sign up for an email newsletter and email input field. if the checkbox is checked but there is no email address entered, i need an example of a function that will check the email address field and provide the error message.
thanks
-
MODX Staff
- 2,502 Posts
Are you looking for a script to run within the eForm snippet? It would be fairly easy to write either or both a javascript and/or php checker for this.
Javascript would be my first inclination since there are lots of email checkers to validate addresses and you would want to do it before the form was posted and add the php to the eForm snippet to check it for those who may have Javascript off.
Let us know if you are looking to add this to eForm or want a standalone script.
Jay
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub
i was thinking i wanted to add a function that would check the values of the fields at &eFormOnValidate.
eForm has a built-in function to check that an email address is valid, so that part is easy (just use the email type for that field). I think I would just make the email field inactive unless the checkbox is checked and use JavaScript to enable it when checked (and empty and disable it if unchecked). You could of course just leave the checkbox out altogether and tell people to enter their email address if they want to sign up for your newsletter (and then you don’t need any extra scripting at all).
i think i figured it out.
function checkEmailNewsletter(&$fields,&$vMsg,&$rMsg){
if ($fields[’contact_email’] == ’’ && $fields[’contact_newsletter_email’] == 1) {
$rMsg[count($rMsg)]="You must provide an email address";
return;
}
}
ok, i think i got it working now but have an additional problem. when testing the true and false scenarios, i get the correct result when the checkbox is checked but there is not email address (i get a validation message). however, when i go back and enter the email address and click submit, the form submits, but i get the following message:
"This form was already submitted successfully. There is no need to submit your information multiple times."
do i need to be setting the parameter &protectSubmit to 1 when running custom validation function within the &eFormOnValidate event to keep this from happening?
thanks
You get this message when you’ve successfully submitted the form already. As you are testing different scenarios you’ve probably triggered this protection preciously. While you are testing turn this off ( &protectSubmit=`0` ), then when you are satisfied with everything turn it back on and do a last test in a new session to see if protectsubmit behaves properly...
protectSubmit works by comparing a checksum of the posted data... Filling in different data in consecutive test may also help...
thank. i actually did all this yesterday and still did not work right. i think i am going to have to leave it turned off and move on.
thanks