FuelPHP - Validation


Advertisements

Validation is one of the frequent and most repeated tasks in a web application. The user enters the required data in the form and submits it. Then, the web application needs to validate the data before processing the data. For example, the user enters the employee data and the post_action needs to validate before saving it into the database. FuelPHP provides a really simple class, Validation for this purpose.

In FuelPHP, the concept of validation is very simple and it provides various methods through Validation class to properly validate the form. Following is the workflow of the validation,

Step 1 − Create new Validation object using forge method.

$val = Validation::forge();

Step 2 − Add the fields which needs to be validated using add method.

$val->add('name', 'Employee name');

Step 3 − Set the validation rules for the added fields using add_rule method.

$val->add('name', 'Employee name')->add_rule('required'); 
$val->add('age', 'Employee age')->add_rule('required') 
   ->add_rule('numeric_min', 20) 
   ->add_rule('numeric_max', 30); 

Step 4 − Call the run method to validate the data.

// run validation on just post 
if ($val->run()) { 
   // success 
} else { 
   // falier 
} 

Step 5 − Use validated and error to get the valid and invalid fields respectively.

$vars = $val->validated(); 
$vars = $val->error();

Rules

FuelPHP contains a lot of rules to validate, and also provides an option to create new rules. The rules supported by the Validation class are as follows,

  • required − Value to be entered

  • required_with − Set another field as a companion field. If the field is set, then companion fields need to be set as well

  • match_value − Set the value to be matched with the field's value

  • match_pattern − Set the value to be matched as a regular expression with the field's value

  • match_field − Set another field's value as value to be matched with the field's value

  • match_collection − Set the value to be matched as collection with field's value

  • min_length − Set the minimum length of the field's value

  • max_length − Set the maximum length of the field's value

  • exact_length − Set the exact length of the field's value

  • valid_date − Set the field's value to a valid date

  • valid_email − Set the field's value to a valid email

  • valid_emails − Set the field's value to valid emails, separated by comma

  • valid_url − Set the field's value to a valid URL

  • valid_ip − Set the field's value to a valid IP

  • numeric_min − Set the minimum value of the field's value

  • numeric_max − Set the maximum value of the field's value

  • numeric_between − Set the minimum and maximum value of the field's value

  • valid_string − Similar to regex but simpler

$val->add('username', 'User name')->add_rule('valid_string', array('alpha, dots');

Here, alpha refers to alphabetical characters and dots refer to (.). The valid string is only the string which contains alphabetical character and (.). The other options are uppercase, lowercase, specials, numeric, spaces, etc.

Working Example

We can update the add employee feature to include validation. Just update the post_add method of the employee controller as follows.

public function post_add() { 
   $val = Validation::forge(); 
   $val->add('name', 'Employee name')->add_rule('required'); 
   $val->add('age', 'Employee age')->add_rule('required')
      ->add_rule('numeric_min', 20) 
      ->add_rule('numeric_max', 30);  
   
   if ($val->run()) { 
      $name = Input::post('name'); 
      $age = Input::post('age');  
      $model = new model_employee(); 
      $model->name = $name; 
      $model->age = $age; 
      $model->save();  
      Response::redirect('employee/list'); 
   } else { 
      Response::redirect('employee/add'); 
   } 
}

Here, we have specified the name and age as the required fields. The age needs to be between 20 and 30. If both rules are valid, then the employee data will be saved and redirected to employee list page. Otherwise, employee data will be rejected and redirected to add employee page.

Advertisements