Validation

The validations allows you to add validation to a certain field. It will print an error message regarding on the validation you set. For more information you can read in Customizer APIs.

Example a text field added required validation, the error message will print if the text field is empty.

How to add validation?

Its easy to add a validation all you need is to add validations and supply it with validations.


Yano::field( 'text', [
   'id'          => 'firstnamedb1',
   'label'       => 'Enter Your Firstname',
   'description' => 'Some description',
   'priority'    => 1,
   'validations' => [ 'required' ]
]);

Example 2: multiple validations, you can add validations as many as you can.


/**
 * This example we will going to use Yano built in validations
 * required - print error message if the field is empty
 * is_integer - print error message if the field value is not integer
 * less_than[18] - print error message if the field value is greater than or equal to "parameter" 18
 */
Yano::field( 'text', [
   'id'          => 'agedb1',
   'label'       => 'Enter Age',
   'description' => 'Some description',
   'priority'    => 1,
   'validations' => [ 'required', 'is_integer', 'less_than[18]' ]
]);

You can also create your own custom function for validations. Note the custom function name must end with _yano_validation example is_number_yano_validation.


/**
 * In this example we will going to print error message if
 * the field value is equal to "John"
 */
Yano::field( 'text', [
   'id'          => 'name',
   'label'       => 'Enter Your Name',
   'description' => 'Some description',
   'priority'    => 1,
   'validations' => [ 'required', 'is_valid_name_yano_validation' ]
]);


/**
 * Note dont for get to add "_yano_validation" in the end custom function name.
 * @param object      $validity        holds your custom error message
 * @param any         $value           the value of the field
 * @return $validity  error message
 */
function is_valid_name_yano_validation( $validity, $value ) {
   if( $value == 'John' ) {
      // printing error message
      $validity->add( 'error', 'John is invalid name.' );
   }
   // dont for get to return $validity
   return $validity;
}

Output:

Here are the list of available Yano validations.

The following are the list of all native validations that are available to use.

required
  • parameter (none)
  • error message - "Required Field"
  • print error message is the value is empty
valid_email
  • parameter (none)
  • error message - "Invalid email address"
  • print error message if the value is not valid email
valid_url
  • parameter (none)
  • error message - "Invalid url"
  • print error message if the value is invalid url
valid_ip
  • parameter (none)
  • error message - "Invalid IP address"
  • print error message if the value is invalid IP Address
numeric
  • parameter (none)
  • error message - "Value must be numeric"
  • print error message if the value is invalid number
is_integer
  • parameter (none)
  • error message - "Invalid integer"
  • print error message if the value contains not integer
min_length
  • parameter (integer)
  • error message - "Characters must not lesser #parameter#"
  • example: min_length[10]
  • print error message if character length is less than #parameter#
max_length
  • parameter (integer)
  • error message - "Characters must not exceed #parameter#"
  • example: max_length[10]
  • print error message if character length is greater than #parameter#
exact_length
  • parameter (integer)
  • error message - "Characters must not exceed #parameter#"
  • example: exact_length[10]
  • print error message if character length is not equal to #parameter#
greater_than
  • parameter (float)
  • error message - "Value must greater than #parameter#"
  • example: greater_than[10]
  • print error message if value is less than or equal to #parameter#
  • note: can be only used to number value
greater_than_equal_to
  • parameter (float)
  • error message - "Value must greater than #parameter#"
  • example: greater_than_equal_to[10]
  • print error message if value is less than to #parameter#
  • note: can be only used to number value
less_than
  • parameter (float)
  • error message - "Value must less than #parameter#"
  • example: less_than[10]
  • print error message if value is greater than or equal to #parameter#
  • note: can be only used to number value
less_than_equal_to
  • parameter (float)
  • error message - "Value must less than #parameter#"
  • example: less_than_equal_to[10]
  • print error message if value is greater than to #parameter#
  • note: can be only used to number value
in_list
  • parameter (string)
  • error message - "Value must be in predermined list #parameter#"
  • example: in_list[apple,grapes,mango]
  • print error message if value is not in predetermined list value
not_in_list
  • parameter (string)
  • error message - "Total words must be exactly #parameter#"
  • example: not_in_list[apple,grapes,mango]
  • print error message if value is in predetermined list value
total_words
  • parameter (integer)
  • error message - "Total words must be exactly #parameter#"
  • example: total_words[2]
  • value total word count is not equal to #parameter#
total_words_greater_than
  • parameter (integer)
  • error message - "Total words must be greater than #parameter#"
  • example: total_words_greater_than[2]
  • value total word count is less than to #parameter#
equal_to_setting
  • parameter (string)
  • error message - "Value must equal to setting #setting_value#"
  • example: equal_to_setting[fullname]
  • value is not equal to set #settings#
  • Note: the parameter is the field id.
not_equal_to_setting
  • parameter (string)
  • error message - "Value must not equal to setting #setting_value#"
  • example: not_equal_to_setting[fullname]
  • value is equal to set #settings#
  • Note: the parameter is the field id.