Input Validation

Validation rules are meant to make sure user's input meets your requirements. To specify validation rules for your form fields, you need to add "validate" parameter to the field's array. You can use one of the predefined rules (see below for a list of available rules), any native PHP function, or you can create your own validation methods and place them in the "includes/validation.php" file.

Lets include the "required" and "validate_license" rules in this example.

<?php
$steps 
= array(
  array(
    
'name' => 'Welcome to the wizard',
    
'fields' => array(
      array(
        
'type' => 'text',
        
'label' => 'License number',
        
'name' => 'license_number',
        
'default' => '0000-0000-0000-0000',
        
'validate' => array(
          array(
'rule' => 'required'),
          array(
            
'rule' => 'validate_license',
            
'params' => array(), // you can include optional parameters here
            
'error' => 'License number does not appear to be valid.'
          
)
        )
      )
    )
  )
);
?>

We now need to include our custom "validate_license" method in the "includes/validation.php" file.

<?php
class Validation extends Validation_Core
{
  function 
validate_license($value$params = array())
  {
    
// note that all form elements values can also be accessed through $_SESSION['params'] array
    
if ( !preg_match('#^[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}$#i'$value) ) {
      
$this->error 'License number does not appear to be valid';
      return 
false;
    }
    return 
true;
  }
}
?>

If user types in the correct license number, our validation method will return "true" and wizard will continue on to the next step. If the license number is not valid, it will return "false" and display an error message.

Note that you can place error messages either in the "includes/steps.php" file along with your validation rule, or in the language file "languages/en.php" by referencing the validation method name. The wizard will then automatically pick it up and display to the user in the event of an error.

<?php
$language 
= array(
  
'validate_license' => 'License number does not appear to be valid.',
);
?>

Also note that you can access any of your form fields using the $_SESSION['params'] array as in the example above.

Available validation rules

The following rules already include error messages in the language file so you don't need to add them (unless you want to change default error messages).

  • required
    Indicates a required field.
  • matches
    Form element must match another one passed in the parameter.
  • min_length
    Form element must not be shorter than the parameter value.
  • max_length
    Form element must not be longer than the parameter value.
  • exact_length
    Form element must be exact length as the parameter value.
  • min_value
    Form element must be a number and greater than the parameter value.
  • max_value
    Form element must be a number and less than the parameter value.
  • exact_value
    Form element must be a number and equal to the parameter value.
  • valid_email
    Form element must be a valid email address.
  • valid_emails
    Form element must be valid comma separated email addresses.
  • valid_ip
    Form element must be a valid IP addess.
  • alpha
    Form element must contain only alphabetic characters.
  • alpha_numeric
    Form element must contain only alpha-numeric characters.
  • alpha_dash
    Form element must contain only alpha-numeric characters, dashes and underscores.
  • numeric
    Form element must contain only numeric characters.
  • integer
    Form element must contain only an integer.
  • is_natural
    Form element must be only a natural number (ie: 0, 1, 2, 3, etc).
  • is_natural_no_zero
    Form element must be only a natural number but not zero (ie: 1, 2, 3, 4, etc).
  • database
    Validations database connection details and database name. Parameter array must contain the following values:
    'params' => array('db_host' => 'db_hostname', 'db_user' => 'db_username', 'db_pass' => 'db_password', 'db_name' => 'db_name')
    You must create the text fields with names equal to the parameter values.