Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate a "does not match" type using CodeIgniter's Form Validation class?

I want to ensure that a certain text field does not contain a specific value. Is there any way for me to do this using CI's Form Validation class or do I have to write my own extension for it?

like image 237
Shamoon Avatar asked Dec 06 '22 03:12

Shamoon


1 Answers

I would extend the form validation class: http://codeigniter.com/user_guide/general/creating_libraries.html

Something like

<?
class MY_Form_validation extends CI_Form_validation {
  function __constuct() {
    parent::__constuct();
  }
  function isnt($str,$field){
    $this->CI->form_validation->set_message('isnt', "%s contains an invalid response");
    return $str!==$field;
  }
}
?>

Your validation rule would be something like

trim|alpha_numeric|isnt[invalid value]

Or, you can create a callback function instead of extending the class. The form validation section of the CI user guide has a relevant example: http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

like image 167
Billiam Avatar answered Apr 12 '23 10:04

Billiam