How can I check the type does not match using the CodeIgniter form validation class?

I want a specific text field to not contain a specific value. Is there a way to do this using the CI form validation class, or do I need to write my own extension for it?

+4
source share
4 answers

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

Sort of

<? 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 will look like

 trim|alpha_numeric|isnt[invalid value] 

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

+8
source

I agree with Billiam that you should extend the Form_validation class

I believe it is more likely that you will want to check the whitelist of possible string values, rather than the blacklist. For example, you know that the field 'shirt_size' should only return string values: 'xl', 'l', 'm', 's'. My solution is to handle both cases.

I use these methods in MY_From_validation:

 /** * ENUM * The submitted string must match one of the values given * * usage: * enum[value_1, value_2, value_n] * * example (any value beside exactly 'ASC' or 'DESC' are invalid): * $rule['order_by'] = "required|enum[ASC,DESC]"; * * example of case-insenstive enum using strtolower as validation rule * $rule['favorite_corey'] = "required|strtolower|enum[feldman]"; * * @access public * @param string $str the input to validate * @param string $val a comma separated lists of values * @return bool */ function enum($str, $val='') { if (empty($val)) { return FALSE; } $arr = explode(',', $val); $array = array(); foreach($arr as $value) { $array[] = trim($value); } return (in_array(trim($str), $array)) ? TRUE : FALSE; } // -------------------------------------------------------------------- /** * NOT ENUM * The submitted string must NOT match one of the values given * * usage: * enum[value_1, value_2, value_n] * * example (any input beside exactly 'feldman' or 'haim' are valid): * $rule['favorite_corey'] = "required|not_enum['feldman','haim']"; * * @access public * @param string $str the input to validate * @param string $val a comma separated lists of values * @return bool */ function not_enum($str, $val='') { return ($this->enum($str,$val) === TRUE)? FALSE : TRUE; } 

Using the Billiam example, the validation rule does not allow the string "invalid value":

 trim|alpha_numeric|not_enum[invalid value] 
+4
source

In fact, there is a fairly simple example given for this very question in the User Guide - for V2 or V3

Find the "Callbacks: Your Own Validation Functions" section. In the example, it uses the verification of the word "test" in the username field and returns a user error if a value is found.

In your controller, change the username rule to the following:

 $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); 

Then add a new function called username_check to your controller:

 function username_check($str) { if ($str == 'test') { $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"'); return FALSE; } else { return TRUE; } } 

And Bob is your uncle ...

+4
source

The CodeIgniter form validation class can call almost any declared PHP function in your rule set. Therefore, I would simply declare a function like this:

 class yourController { function someFunction() { $this->form_validation->set_rules('the_field_you_want_to_check', 'The Field Name', 'trim|myvalfunc[not this value]|xss'); } } function myvalfunc($formvalue, $notallowed) { $this->CI->form_validation->set_message('myvalfunc', "%s is not allowed"); return $formvalue !== $nowallowed; } 
0
source

Source: https://habr.com/ru/post/1307450/


All Articles