2010-04-20 70 views

回答

8

我将扩展表单验证类: http://codeigniter.com/user_guide/general/creating_libraries.html

喜欢的东西

<? 
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; 
    } 
} 
?> 

你的验证规则会是这样的

trim|alpha_numeric|isnt[invalid value] 

或者,您可以创建一个回调函数,而不是扩大班级。在CI用户指南的形式验证部分有一个相关的例子: http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

+1

很好的答案......但有两件事。首先,'$ this-> CI-> form_validation'是不必要的,因为你已经在该类中工作了。只要执行'$ this-> set_message()'。其次,当你扩展一个类时,不需要构造函数,除非你在该构造函数中做某件事。在这种情况下,在这里调用它就和不调用它一样。 – bschaeffer 2010-05-18 14:19:41

4

我Billiam同意,你应该扩展Form_validation类

我发现它更可能是一个会想验证涂白可能的字符串值列表,而不是黑名单。例如,你知道你的'shirt_size'字段应该只返回字符串值:'xl','l','m','s'。我的解决方案是处理这两种情况。

我使用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; 
    } 

使用Billiam的例子中,验证规则不允许字符串“无效值”将是这样的:

trim|alpha_numeric|not_enum[invalid value] 
0

CodeIgniter的表单验证类可以调用几乎任何声明的PHP函数都在您的规则集中。所以我会简单地声明函数像这样:

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; 
} 
4

其实,还有一个供用户指南在这个问题给出一个相当简单的例子 - 为V2V3

查找部分“回调:您自己的验证功能“。在该示例中,它在用户名字段中检查单词“test”,如果找到该值,则返回自定义错误。

在你的控制,改变 “用户名” 的规则是:

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

然后一个新的函数调用username_check添加到您的控制器:

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; 
    } 
} 

和鲍勃的你的叔叔...

+0

网址[http:// ellislab。com/codeigniter/user-guide/libraries/form_validation.html](http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html)应该更改 – Eranda 2014-05-13 01:31:12

+0

应该是 [https://www.codeigniter .com/userguide2/libraries/form_validation.html#callbacks](https://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks)for v2 or [https://www.codeigniter.com/userguide3 /libraries/form_validation.html#callbacks-your-own-validation-methods](https://www.codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods)for v3 – richplane 2017-07-10 14:38:26

+0

已更新 - 感谢您的正确链接! – 2017-07-14 14:21:59