This question has been answered by Steeve. See the first response.
[[!FormIt? ... ... &customValidators=`testEmail` ... ... &validate=`emailField:testEmail`
$email = $value;
$findme = 'noreply';
$pos = strpos($email, $findme);
if ($pos === false) {
$success = false;
} else {
$success = true;
}
if ($success == false) {
// Note how we can add an error to the field here.
$validator->addError($key,'Email with noreply is not accepted ');
}Hi,
You can make your own validators with Formit extra. You can find documentation here :
https://docs.modx.com/extras/revo/formit/formit.validators#FormIt.Validators-CustomValidators
Formit call example :
[[!FormIt? ... ... &customValidators=`testEmail` ... ... &validate=`emailField:testEmail`
Then you make a snippet named testEmail in my example with something like this inside :
$email = $value; $findme = 'noreply'; $pos = strpos($email, $findme); if ($pos === false) { $success = false; } else { $success = true; } if ($success == false) { // Note how we can add an error to the field here. $validator->addError($key,'Email with noreply is not accepted '); }
1. When I fall out of this routine, I'll need a return $success statement, yes? That's almost obvious but I'm not a PHP programmer so I'll ask you anyway.You do the same to with a success == true like this :
if ($success == false) {
// Note how we can add an error to the field here.
$validator->addError($key,'Email with noreply is not accepted ');
}else if($success == true){
// Note how we can add an success to the field here.
$validator->addError($key,'Good email ! ');
}3. If I want to also check for a second string such as "donotreply" in addition to "noreply" can you suggest the cleanest way to test for either string?
$findme = array('noreply', 'donotreply', 'othertest');4. As a final question, since I don't understand your if ($pos === false) test, it seemed to me that $pos is false if the string is NOT found, in which case $success would be true. I'm probably wrong but I needed to ask.The pos PHP function definition is : Find the position of the first occurrence of a substring in a string.
Steeve, if I'm wasting your time by asking these additional newbie questions it's OK if you don't answer them. I'll get to my solution on my own from the significant assistance you've already provided me.That's the MODX community ;-) [ed. note: Steeve last edited this post 9 years, 11 months ago.]
Thanks again Steeve, especially for such a quick response.
I'm glad my post helped you @samg !
1. When I fall out of this routine, I'll need a return $success statement, yes? That's almost obvious but I'm not a PHP programmer so I'll ask you anyway.You do the same to with a success == true like this :
if ($success == false) { // Note how we can add an error to the field here. $validator->addError($key,'Email with noreply is not accepted '); }else if($success == true){ // Note how we can add an success to the field here. $validator->addError($key,'Good email ! '); }
EDIT : This is wrong! The form with adderror is not submited. Sorry for this!
3. If I want to also check for a second string such as "donotreply" in addition to "noreply" can you suggest the cleanest way to test for either string?
$findme = array('noreply', 'donotreply', 'othertest');4. As a final question, since I don't understand your if ($pos === false) test, it seemed to me that $pos is false if the string is NOT found, in which case $success would be true. I'm probably wrong but I needed to ask.The pos PHP function definition is : Find the position of the first occurrence of a substring in a string.
Steeve, if I read this correctly, the form will not be submitted if we have a return false OR if we have a $validator addError statement, even if the return is true. Yes?True! The form will not be submitted.
Thank you Steeve. This way of testing multiple tests is much better than anything I would have come up with!You're welcome!
I think you're agreeing with me Steeve. If ($pos === false) in your example, I should set $success === true because no bad string was found and we want the form submitted. You accidentally had set $success === false if ($pos === false).You're right!
I guess that's about it Steeve. I'll try out all your suggestions and see how I do.You're welcome! Again!
Thanks again ... Sam Gerber (samg)
You're welcome! Again!
<?php
/* reject email addresses containing noreply, donotreply */
$email = $value;
if (strpos($email, 'noreply') !== false || strpos($email, 'donotreply') !== false) {
return false;
}
else {
return true;
}
Quote from: Steeve at Oct 28, 2016, 03:42 AMI never tried to name validator like this (with a dot). Maybe it's better to try camel name style like this : filterNoReplyAddress. It's maybe better to put your custom validator before required filter :
You're welcome! Again!
Steeve, if you're still following this thread, I need to run something by you. I tested my customer validator and it should have rejected the form but instead it let it go through. I'm sure I have some syntax problem or logic problem. I'll try to be a brief as possible here.
First, here are my 2 statements in the Formit call:
[[!FormIt?
&emailSubject=`Request from ELC Join Our Network Form`
&customValidators=`filter.noreplyaddresses`
&validate=`nospam:blank,name:required,email:email:required:filter.noreplyaddresses`
&successMessage=`<style>#joinournetwork{display:none;}</style><h3>THANK YOU!</h3><p>Your information was successfully submitted.</p>`
Is it OK to name the validator snippet as I did? Also, if I'm performing 3 tests on the 'email;' field, is it OK to do it the way I did it? That is by using the colon 3 times in email:email:required:filter.noreplyaddresses.
It's just to be more readable by human. I prefer to know i test an email variable than to test a value variable. But you can eliminate this line and change all $email to $value. $value contain the value applied by the filter.
Finally I'd like to show you my snippet, filter.noreplyaddresses.
<!--?php /* reject email addresses containing noreply, donotreply */ $email = $value; if (strpos($email, 'noreply') !== false || strpos($email, 'donotreply') !== false) { return false; } else { return true; }
I used your example when I wrote $email = $value;
I basically didn't understand why I wrote this. In looking back at it it looks like I'm changing the email form's value to whatever was in the $value filed which was probably null. Did I actually set my 'email' field to blanks or null? Should I have eliminated the statement $email = $value;?
No problemQuote from: samg at Oct 28, 2016, 04:51 PM
I guess that's it, Steeve. When I tested the form I used an email address of [email protected].
Sorry to burden you with this but I didn't want to start a new thread.
You're welcome
Thanks for any further help ... Sam-->
Quote from: samg at Oct 28, 2016, 04:51 PMQuote from: Steeve at Oct 28, 2016, 03:42 AMI never tried to name validator like this (with a dot). Maybe it's better to try camel name style like this : filterNoReplyAddress. It's maybe better to put your custom validator before required filter :
You're welcome! Again!
Steeve, if you're still following this thread, I need to run something by you. I tested my customer validator and it should have rejected the form but instead it let it go through. I'm sure I have some syntax problem or logic problem. I'll try to be a brief as possible here.
First, here are my 2 statements in the Formit call:
[[!FormIt?
&emailSubject=`Request from ELC Join Our Network Form`
&customValidators=`filter.noreplyaddresses`
&validate=`nospam:blank,name:required,email:email:required:filter.noreplyaddresses`
&successMessage=`<style>#joinournetwork{display:none;}</style><h3>THANK YOU!</h3><p>Your information was successfully submitted.</p>`
Is it OK to name the validator snippet as I did? Also, if I'm performing 3 tests on the 'email;' field, is it OK to do it the way I did it? That is by using the colon 3 times in email:email:required:filter.noreplyaddresses.
email:email:filter.noreplyaddresses:required
Quote from: samg at Oct 28, 2016, 04:51 PMIt's just to be more readable by human. I prefer to know i test an email variable than to test a value variable. But you can eliminate this line and change all $email to $value. $value contain the value applied by the filter.
Finally I'd like to show you my snippet, filter.noreplyaddresses.
<!--?php /* reject email addresses containing noreply, donotreply */ $email = $value; if (strpos($email, 'noreply') !== false || strpos($email, 'donotreply') !== false) { return false; } else { return true; }
I used your example when I wrote $email = $value;
I basically didn't understand why I wrote this. In looking back at it it looks like I'm changing the email form's value to whatever was in the $value filed which was probably null. Did I actually set my 'email' field to blanks or null? Should I have eliminated the statement $email = $value;?
For more example : https://docs.modx.com/extras/revo/formit/formit.validators#highlighter_595530
Quote from: samg at Oct 28, 2016, 04:51 PMNo problemQuote from: samg at Oct 28, 2016, 04:51 PM
I guess that's it, Steeve. When I tested the form I used an email address of [email protected].
Sorry to burden you with this but I didn't want to start a new thread.
You're welcome
Thanks for any further help ... Sam-->