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?
views:
229answers:
4I 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
I agree with Billiam that you should extend the Form_validation class
I find it is more likely that one would want to validate a white-list of possible string values rather than a black-list. For example, you know your 'shirt_size' field should only return the 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 Billiam's example, the validation rule not allow string 'invalid value' would be something like:
trim|alpha_numeric|not_enum[invalid value]
CodeIgniter's form validation class can call almost any declared PHP function in your rule set. So I would simply declare a function like so:
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;
}
Actually, there's a fairly simple example given for this very question in the User Guide - http://codeigniter.com/user_guide/libraries/form_validation.html
Scroll down about halfway and you'll see the section "Callbacks: Your own Validation Functions". In the example it uses a check for the word "test" in the username field, and returns the custom error if the value is found.
In your controller, change the "username" rule to this:
$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's your uncle...