How to create custom validation rule?
-
Hi,
I was wondering if it is possible to create a custom validation rule with custom error message? Example: I am trying to create a rule that says the field “name-1” must have at least 2 words in it. Is such thing possible to do?
-
I hope you’re well today!
There is currently now such option to create fully custom validation rules.
Some of such rules could possibly be achieved simply by setting visibility conditions for “submit” field. For example, let’s say that you only wanto to allow e-mail field in “gmail.com” domain only: you can set a visibility rule for “Submit” button to show button only if email field value ends with “gmail.com”.
So that’s partial workaround but the downside is that it will not handle all scenarios (e.g. it cannot count words or characters) and it will simply hide “Submit” button rather than showing error.
—-
To add more advanced validation and be able to display custom errors, you would need to go for additional custom code. here is example code that will show error (and block form submission) if name-1 field has less than 2 words in it:
<?php add_filter( 'forminator_custom_form_submit_errors', 'check_textarea_minimum_words', 99, 3 ); function check_textarea_minimum_words( $submit_errors, $form_id, $field_data_array ) { # CONFIG HERE $form_ids = array( 2688 ); // IDs of forms to check; if more than one, separate by comma $text_field = 'name-1'; // text or textarea field ID $text_words = 2; // minimum number of words to allow $error_msg = 'Text too short!'; // error message # END OF CONFIG if ( in_array( $form_id, $form_ids ) ) { $words = 0; foreach( $field_data_array as $arr ) { if ( $arr['name'] == $text_field ) { $text_val = $arr['value']; $words = preg_split( '/[\s]+/', $arr['value'] ); if ( is_array( $words ) && count( $words ) < $text_words ) { $submit_errors[][$text_field] = $error_msg; } } } } return $submit_errors; }To use it on site:
– create an empty file with a .php extension (e.g. “formiantor-custom-validations.php”)
– copy and paste code into that file
– in these lines$form_ids = array( 2688 ); // IDs of forms to check; if more than one, separate by comma $text_field = 'name-1'; // text or textarea field ID $text_words = 2; // minimum number of words to allow $error_msg = 'Text too short!'; // error messageconfigure it accordingly as explained in the comments
– save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation.
Note: this is a custom code and only validates for this particular case but it can be expanded to validate other fields too but if you need some more “complex” validations to be made by it, you may need to consider hiring a developer to code it for you as we can only provide limited scope of custom coding.
Best regards,
AdamHi!
Thanks for the explanation!
But can it be maybe done with javascript?
In theory, yes – it should be partially doable with JS only. In practice, this is more reliable, robust and simpler solution as with JS there are also additional factors that would need to be taken into account: Forminator is already heavily based on JS and this may create some conflicts, some data is additionally sanitized or injected using AJAX requests only and relying on CSS selectors (which JS would do) is not the best idea here due to how Forminator creates forms.
In general, it’s doable but providing custom solution “on demand” for this is out of the scope of this support, I’m afraid.
Kind regards,
AdamDont bother, it works!
Thanks for all the help and wish you the best!
The topic ‘How to create custom validation rule?’ is closed to new replies.