我想确保某个文本字段不包含特定的值。有没有办法让我使用CI的表格验证课程来做到这一点,还是我必须为它编写自己的扩展名?如何使用CodeIgniter的Form Validation类验证“不匹配”类型?
回答
我将扩展表单验证类: 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
我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]
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;
}
其实,还有一个供用户指南在这个问题给出一个相当简单的例子 - 为V2或V3
查找部分“回调:您自己的验证功能“。在该示例中,它在用户名字段中检查单词“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;
}
}
和鲍勃的你的叔叔...
网址[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
应该是 [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
已更新 - 感谢您的正确链接! – 2017-07-14 14:21:59
- 1. 如何验证数据类型不匹配? (不是=== false)
- 2. 如何验证值相匹配的数据类型的Python
- 3. Spring Form Validation验证数据库插入?
- 4. Codeigniter中的哪种类型验证?
- 5. 泛型类型和类型不匹配
- 6. 类型参数的类型不匹配
- 7. 类泛型的类型不匹配
- 8. 如何匹配byte []类型?
- 9. 对象与SetValue使用类型不匹配的目标类型
- 10. 使用较高主干类型时的类型不匹配
- 11. Scala的类型不匹配
- 12. XQuery的类型不匹配
- 13. 使用Kotlin ArrayOfNulls类型不匹配
- 14. 使用Numpy's ifft时类型不匹配
- 15. 匹配类型
- 16. AS2 addCallback类型不匹配
- 17. 匹配类型
- 18. 类型与Data.PSQueue不匹配
- 19. VBA类型不匹配(13)
- 20. HTMLCanvasElement类型不匹配
- 21. 类型与glLightfv不匹配
- 22. 类型不匹配:Server.HTMLEncode'ASP
- 23. 类型不匹配:SiteEdit.ComponentPresentation
- 24. 协会类型不匹配
- 25. 类型不匹配 - JavaScript
- 26. 如何使用jQuery验证验证日期类型字段
- 27. JSON类型不匹配
- 28. VBA Excel类型不匹配
- 29. VBScript类型不匹配
- 30. Scala mapValues()类型不匹配
很好的答案......但有两件事。首先,'$ this-> CI-> form_validation'是不必要的,因为你已经在该类中工作了。只要执行'$ this-> set_message()'。其次,当你扩展一个类时,不需要构造函数,除非你在该构造函数中做某件事。在这种情况下,在这里调用它就和不调用它一样。 – bschaeffer 2010-05-18 14:19:41