├── libraries └── Jquery_validation.php └── readme.md /libraries/Jquery_validation.php: -------------------------------------------------------------------------------- 1 | 'required', 7 | 'matches' => 'equalTo', 8 | 'min_length' => 'minlength', 9 | 'max_length' => 'maxlength', 10 | 'greater_than' => 'min', 11 | 'less_than' => 'max', 12 | 'numeric' => 'digits', 13 | 'valid_email' => 'email' 14 | ); // CI rule names to jQuery validator rules 15 | /** 16 | * Build and return jQuery validation code 17 | * 18 | * @access public 19 | * @param string 20 | * @return string 21 | */ 22 | public function run($form_name) 23 | { 24 | return $this->build_script($form_name); 25 | } 26 | 27 | /** 28 | * Build jQuery validationcode 29 | * 30 | * @access private 31 | * @param string 32 | * @return string 33 | */ 34 | private function build_script($form_name) 35 | { 36 | $script = '$(document).ready(function() { $("'.$form_name.'").validate({rules: %s,messages: %s});});'; 37 | return sprintf($script, $this->rules, ($this->messages ? $this->messages : '{}')); 38 | } 39 | /** 40 | * Set validation rules 41 | * 42 | * @access public 43 | * @param array 44 | * @return string json formatted string 45 | */ 46 | public function set_rules($rules) 47 | { 48 | foreach ($rules as $k => $v) 49 | { 50 | // CI uses "|" delimiter to apply different rules. Let's split it ... 51 | $expl_rules = explode('|', $v['rules']); 52 | foreach ($expl_rules as $index => $rule) 53 | { 54 | // check and parse rule if it has parameter. eg. min_length[2] 55 | if (preg_match("/(.*?)\[(.*)\]/", $rule, $match)) 56 | { 57 | // Check if we have similar rule in jQuery plugin 58 | if($this->is_js_rule($match[1])) 59 | { 60 | // If so, let's use jQuery rule name instead of CI's one 61 | $json[$v['field']][$this->get_js_rule($match[1])] = $match[2]; 62 | } 63 | } 64 | // jQuery plugin doesn't support callback like CI, so we'll ignore it and convert everything else 65 | elseif (!preg_match("/callback\_/",$rule)) 66 | { 67 | if($this->is_js_rule($rule)) 68 | { 69 | $json[$v['field']][$this->get_js_rule($rule)] = TRUE; 70 | } 71 | } 72 | } 73 | } 74 | $this->rules = json_encode($json); 75 | return $this->rules; 76 | } 77 | 78 | /** 79 | * check if we have alternative rule of CI in jQuery 80 | * 81 | * @access private 82 | * @param string 83 | * @return bool 84 | */ 85 | private function is_js_rule($filter) 86 | { 87 | if (in_array($filter,array_keys($this->js_rules))) 88 | { 89 | return TRUE; 90 | } 91 | else 92 | { 93 | return FALSE; 94 | } 95 | } 96 | 97 | /** 98 | * Get rule name 99 | * 100 | * get jQuery rule name by CI rule name 101 | * 102 | * @access private 103 | * @param string 104 | * @return string 105 | */ 106 | private function get_js_rule($filter) 107 | { 108 | return $this->js_rules[$filter]; 109 | } 110 | 111 | /** 112 | * Set messages 113 | * 114 | * set custom error messages on each rule for jQuery validation 115 | * 116 | * @access public 117 | * @param array 118 | * @return string json formated string 119 | */ 120 | public function set_messages($messages) 121 | { 122 | // We do same as above in set_rules function check and convert CI to jQuery rules 123 | foreach ($messages as $k=>$v) 124 | { 125 | foreach ($v as $a=>$v) 126 | { 127 | if ($this->is_js_rule($a)) 128 | { 129 | // Remove CI rule name ... 130 | unset($messages[$k][$a]); 131 | // and insert jQuery's one 132 | $messages[$k][$this->get_js_rule($a)] = $v; 133 | } 134 | } 135 | } 136 | $this->messages = json_encode($messages); 137 | return $this->messages; 138 | } 139 | } 140 | // END Jquery_validation class 141 | 142 | /* End of file Jquery_validation.php */ 143 | /* Location: ./application/libraries/Jquery_validation.php */ 144 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## CodeIgniter jQuery validator library 2 | 3 | This is very simple library which generates Javascript code for forms validation 4 | 5 | It generates validation rules and custom error messages for [jQuery Validation Plugin](http://bassistance.de/jquery-plugins/jquery-plugin-validation/) 6 | 7 | ## Usage 8 | 9 | 1) Load library in your controller. 10 | 11 | ```php 12 | $this->load->library('jquery_validation'); 13 | ``` 14 | 15 | 2) Set CodeIgniter standard validation rules ( The same rules format which has [form_validtion library](http://codeigniter.com/user_guide/libraries/form_validation.html#validationrulesasarray) ). 16 | 17 | ```php 18 | $rules = array( 19 | array( 20 | 'field' => 'username', 21 | 'label' => 'Username', 22 | 'rules' => 'required|min_length[2]' 23 | ), 24 | array( 25 | 'field' => 'email', 26 | 'label' => 'Email', 27 | 'rules' => 'required|valid_email' 28 | ), 29 | array( 30 | 'field' => 'url', 31 | 'label' => 'URL', 32 | 'rules' => 'required' 33 | ) 34 | ); 35 | ``` 36 | 37 | 3) Set error messages. 38 | 39 | ```php 40 | $messages = array( 41 | 'username' => array( 'required' => "Username is required", 42 | 'min_length' => "Please enter more then 2 char" 43 | ), 44 | 45 | 'email' => array( 'required' => "Email is required", 46 | 'valid_email' => "Please enter valid email" 47 | ) 48 | ); 49 | 50 | ``` 51 | 52 | 4) Apply validation rules and messages to library. 53 | 54 | ```php 55 | 56 | $this->jquery_validation->set_rules($rules); 57 | $this->jquery_validation->set_messages($messages); 58 | 59 | ``` 60 | 61 | 5) Generate Javascript validation code. 62 | 63 | ```php 64 | // pass css selector for your form to run method 65 | 66 | $validation_script = $this->jquery_validation->run('#registration_form'); 67 | 68 | // echo $validation_script in your