杂货CRUD: set_rules回调不起作用


GROCERY CRUD: set_rules callbacks not working

我尝试使用回调函数验证密码字段。但是当我使用以下代码时,当我键入1234(回调条件)时,所有验证都不再工作。当我删除包含回调函数的验证时,其他验证就可以正常工作了。

这是我的验证规则

    $crud->set_rules('password', 'Password', 'callback_valid_password');
    $crud->set_rules('confirm_password', 'Password Confirmation', 'required|matches[password]');
    $crud->set_rules('email', 'Email', 'trim|required|valid_email');

这是我的回调函数

    function valid_password($str) {
    if ($str=="1234")
    {
        $crud->set_message('_valid_password', 'The field should be 1234');
        //do some pw validation
        return FALSE;
    }
    else
    {
        return TRUE;
    }
    }

请帮我找出这里有什么问题。先谢谢你p.s -我使用php 5.4版本与最新的杂货杂货版本

function valid_password($str) {
   if ($str=="1234")
   {
      $this->form_validation->set_message('valid_password', 'The field should be 1234');
      //do some pw validation
      return FALSE;
   }
   else
   {
      return TRUE;
   }
}

对于那些仍在努力寻找解决方案的人,请遵循清单。

您是否使用CodeIgniter作为MVC或HMVC?

1。HMVC

(A) -检查您是否已经更新了文件(./application/libraries/Grocery_crud.php)如下所示。

(B) -在"__construct" inside "class Grocery_CRUD extends grocery_CRUD_States" add "protected $hmvc;"

(C) -更新"__construct"如下:

public function __construct($hmvc = null)
{
    $this->hmvc = $hmvc;
}

(D) -更新"form_validation"如下:

protected function form_validation()
{
    if ($this->form_validation === null) {
        $this->form_validation = new grocery_CRUD_Form_validation();
        if ($this->hmvc) $this->form_validation->CI = $this->hmvc;
        $ci = &get_instance();
        $ci->load->library('form_validation');
        $ci->form_validation = $this->form_validation;
    }
    return $this->form_validation;
}

(E) -在你的控制器中使用"$crud = new Grocery_crud($this);"代替"$crud = new Grocery_crud();"。

(F) - GC set_rules示例:

$crud->set_rules("level_title", 'Level Title Label', 'trim|required|callback_unique_level_field_check');

(G) - 回调方法的例子:

public function unique_level_field_check ($level_title)
{
    if ( empty($level_title))
        {
            $this->form_validation->set_message('unique_level_field_check', "Level Title Label should be unique");
            return FALSE;
        }
    return TRUE;
}

2。MVC

跟随F &


GroceryCRUD论坛:查看详细信息