【问题标题】:Cakephp validation errorCakephp 验证错误
【发布时间】:2016-07-02 07:30:01
【问题描述】:

控制器代码

if(!empty($this->data)){ if($this->{$this->modelClass}->signupValidate()){ $this->{$this->modelClass}->save($this->data); } }

型号代码

函数注册验证(){ $验证1 =数组( 'first_name' => 数组( 'rule1' => 数组( '规则' => 'notEmpty', 'message' => __('请输入名字',true) ) ) ); $this->验证 = $validate1; 返回 $this->validates(); }

验证无法正常工作

【问题讨论】:

  • 请显示错误日志并告诉您要做什么。
  • 已解决 $this->{$this->modelClass}->set($this->data);这一行

标签: validation cakephp


【解决方案1】:

你应该在你的控制器中设置

$this->{$this->modelClass}->set($this->data);

这样

if(!empty($this->data)){ $this->{$this->modelClass}->set($this->data); if($this->{$this->modelClass}->signupValidate()){ $this->{$this->modelClass}->save($this->data); } }

【讨论】:

    【解决方案2】:

    您的模型会在保存数据之前自动调用验证,如果没有,那么您可以在控制器中使用以下代码

    在你的控制器中

    $this->loadModel('YourModel');
    if($this->YourModel->validates())
    {
        $this->YourModel->save($this->data); 
    }
    

    在您的模型类中添加以下代码

    var $validate = array(
            'first_name' => array(
                'notEmpty'  => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.'
                )
            ),
            'last_name' => array(
                'notEmpty'  => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.'
                )
            ),
            'phone' =>  array(
                'notEmpty'  => array(
                    'rule' => 'notEmpty',
                    'message' => 'Phone number should be valid.'
                ),
                'phone' => array(
                    'rule' => array('phone', null, 'us'),
                    'message' => 'Phone number should be valid e.g. 555-555-5555'
                )
            ),
            'email' => array(
                'email' => array(
                    'rule' => 'email',
                    'message' => 'Please enter a valid email address'
                ),
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank'
                ),
                'validEmail' => array(
                    'rule' => array('validEmail'),
                    'message' => 'Email address does not exist.'
                )
            ),
            'captcha_code' => array(
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank'
                )
            ),
            'address' => array(
                'notEmpty'  => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.'
                )
            ),
            'city' => array(
                'notEmpty'  => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.'
                )
            ),
            'street' => array(
                'notEmpty'  => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.'
                )
            )
    
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2016-05-10
      相关资源
      最近更新 更多