How to create custom validation rule?

Hi @svenratkajec

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 message

configure 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,
Adam

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer