【问题标题】:GROCERY CRUD: set_rules callbacks not workingGROCERY CRUD:set_rules 回调不起作用
【发布时间】:2014-05-12 16:04:38
【问题描述】:

我尝试使用回调函数验证密码字段。但是当我使用以下代码时,当我键入 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 版本和最新的杂货杂货版本

【问题讨论】:

    标签: php codeigniter validation crud grocery-crud


    【解决方案1】:
    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;
       }
    }
    

    【讨论】:

      【解决方案2】:

      对于那些仍在努力寻找解决方案的人,请按照清单进行操作。

      您使用 CodeIgniter 作为 MVC 还是 HMVC?

      1. HMVC

      (A) - 检查您是否已按照以下建议更新文件 (./application/libraries/Grocery_crud.php)。

      (B) - 在“class Grocery_CRUD extendsgrocery_CRUD_States”内的“__construct”之前添加“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 和 G(上方)。


      GroceryCRUD 论坛:See details here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-13
        • 2013-11-20
        • 2018-04-08
        • 2015-11-09
        • 2012-03-19
        • 2012-10-09
        相关资源
        最近更新 更多