php - CodeIgniter form validation rule won't run if string is empty -
i have custom rule, regex[table_name.column_name.post_value]
, using in following manner:
$this->form_validation->set_rules( 'postal_code', 'postal code', 'regex[countries.postal_code_regex.country_code]' );
and here custom rule's function in my_form_validation.php:
public function regex($str, $field){ list($table, $col, $post_val)=explode('.', $field); // grab regex $regex = $this->ci ->db ->limit(1) ->select($col) ->where($post_val, $_post[$post_val]) ->get($table) ->row($col); return preg_match('/'.$regex.'/', $str) === 1; }
how codeigniter run rule if user sends empty string?
note: cannot use required
rule depends on whether database has regex or not.
try below code:
public function regex($str, $field){ list($table, $col, $post_val)=explode('.', $field); // grab regex $regex = $this->ci ->db ->limit(1) ->select($col) ->where($post_val, $_post[$post_val]) ->get($table) ->row($col); if(trim($str) === "") $str = 1; return preg_match('/'.$regex.'/', $str) === 1; }
Comments
Post a Comment