Advertisement
VladimirsBudkins

CI validation rules

Mar 2nd, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. class Some_controller extends CI_Controller {
  2.  
  3.     public validate(){
  4.         $rules = $this->some_model->get_custom_rules(array('rule1','rule2'));
  5.         $this->form_validation->set_rules($rules);
  6.     }
  7.  
  8. }
  9.  
  10. class Some_model extends CI_Model{
  11.     public $validation_rules = array(
  12.             array('field' => 'rule1', 'label' => 'label1', 'rules' => 'required|max_length[255]'),
  13.             array('field' => 'rule2', 'label' => 'label2', 'rules' => 'required|max_length[255]'),
  14.         );
  15.  
  16.     public function get_custom_rules(array $componets) {
  17.             $rules = array();
  18.             foreach ($this->validation_rules as $value) {
  19.                     if (in_array($value['field'], $componets)) {
  20.                         $rules[] = $value;
  21.                     }
  22.             }
  23.             return $rules;
  24.         }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement