我如何验证一个";“不匹配”;使用CodeIgniter'键入;什么是表单验证类?
我希望确保某个文本字段不包含特定值。我是否可以使用CI的表单验证类执行此操作,或者我必须为其编写自己的扩展?我将扩展表单验证类: 差不多我如何验证一个";“不匹配”;使用CodeIgniter'键入;什么是表单验证类?,codeigniter,validation,Codeigniter,Validation,我希望确保某个文本字段不包含特定值。我是否可以使用CI的表单验证类执行此操作,或者我必须为其编写自己的扩展?我将扩展表单验证类: 差不多 <? class MY_Form_validation extends CI_Form_validation { function __constuct() { parent::__constuct(); } function isnt($str,$field){ $this->CI->form_validati
<?
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用户指南》的表单验证部分有一个相关示例:
我同意Billiam的观点,即您应该扩展Form\u验证类 我发现,人们更可能希望验证可能字符串值的白名单,而不是黑名单。例如,您知道“衬衫尺寸”字段应该只返回字符串值:“xl”、“l”、“m”、“s”。我的解决办法是处理这两种情况 我在我的_From_验证中使用以下方法:
/**
* 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]
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;
}
实际上,在《用户指南》中有一个非常简单的例子来说明这个问题——for or 查找“回调:您自己的验证函数”部分。在本例中,它在用户名字段中使用“test”一词的检查,如果找到该值,则返回自定义错误 在控制器中,将“用户名”规则更改为: 然后将名为username\u check的新函数添加到控制器:
鲍勃是你叔叔……回答得好。。。但有两件事。首先,
$this->CI->form_validation
是不必要的,因为您已经在该类中工作了。只需执行$this->set_message()
。第二,扩展类时不需要构造函数,除非您在构造函数中执行某些操作。在这种情况下,在这里调用它与不调用它是一样的。url应该更改,应该是v2或v3
$this->form_validation->set_rules('username', 'Username', 'callback_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;
}
}