密码不能是CodeIgniter表单验证中的用户名

时间:2014-05-29 22:48:33

标签: php codeigniter

注册时,我想要我的密码'字段具有以下自定义规则:

not_matches[username]

然后我会设置以下语言:

$lang['not_matches'] = "The %s field cannot be the same as the %f field";

(假设%f是字段名称)

这可能吗?

要明确,我知道如何做not_matches[".$val."]但我想要一个灵活的规则。

3 个答案:

答案 0 :(得分:0)

我不确定我明白你在说什么。你想要一个规则,说明not_matches ['username']是另一个输入字段的用户名吗?

如果是这样,只需转到system \ libraries \ form_validation.php,然后找到匹配规则并复制它,将==更改为!==,将名称更改为not_matches。然后转到system \ language \ english \ form_validation_lang.php并创建消息。

答案 1 :(得分:0)

CI中还没有这样的支持。您需要编写自己的回调验证例程。

有一个例子可以在CI manual中轻松适应您的需求。

答案 2 :(得分:0)

我是这样做的。

应用/语言/英语/ form_validation_lang.php

$this_lang = basename(__DIR__);
require_once("./system/language/$this_lang/form_validation_lang.php");

$lang['not_match'] = "The %s field must not match the %s field.";

应用/库/ MY_Form_validation.php

class MY_Form_validation extends CI_Form_validation {

  function __construct()
  {
    parent::__construct();
  }

  public function not_match($str, $field)
  {
    if ( ! isset($_POST[$field]))
    {
      return FALSE;
    }

    $field = $_POST[$field];

    return ($str === $field) ? FALSE : TRUE;
  }

}

然后规则只是not_match[field_name]

相关问题