【问题标题】:cakePHP isUnique is not working any morecakePHP isUnique 不再工作
【发布时间】:2014-04-23 10:51:05
【问题描述】:

我有这个验证规则很长时间了,它总是能正常工作,但现在不行了。

  public $validate = array(
            'email'=>array(
                'email'=>array(
                    'rule'=>array('email', true),
                    'message'=>'E-mail is not correct'
                ),
                'isUnique'=>array(
                    'rule'=>'isUnique',
                    'message'=>'This email adddress is already in use'
                ),
                'notEmpty'=>array(
                    'rule'=>'notEmpty',
                    'message'=>'Insert email address'
                )
            )

我一直在测试许多可能的方法,但似乎都没有。非常感谢任何帮助。

这是来自控制器的添加函数:

公共函数添加(){

if ($this->request->is('post')) {

if( $this->request->data[ 'User' ][ 'email' ] != $this->data[ 'User' ][ 'email_confirmation' ] ) {
    $this->User->invalidate( 'email_confirmation', __('Emails dont match'));
}else{

//generate password

$generatedPassword = $this->getRandomString(6);

$this->request->data['User']['password'] = $generatedPassword;


if ($this->User->save($this->request->data)) {

    $name = $this->request->data['User']['name'];
    $email = $this->request->data['User']['email'];
    $password = $generatedPassword;


        $this->Session->setFlash(__('User has been saved'), 'positive_notification');

        $this->redirect(array('action' => 'index'));

    } else {
        $this->Session->setFlash(__('User has not been saved'), 'negative_notification');
    }
    }}}

这里是 beforeSave()

public function beforeSave($options = Array()){
    if(isset($this->data['User']['password'])){
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    if(isset($this->data[$this->alias]['password1'])){
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password1']);
    }   
    return true;
}

这是保存时正在执行的查询:

INSERT INTO `schedule`.`users` (`employee_nr`, `name`, `surname`, `email`, `phone`, `address`, `post_nr`, `city`, `role`, `company_id`, `password`) VALUES ('1', 'Name', 'Surname', 'email@email.com', '11223344', '', '', '', 'employee', 54, '389be16c83bf2e65a64f727465ec0c37a77552a5')

【问题讨论】:

  • 我已经尝试过创建和更新,只是没有在电子邮件字段上检查 isUnique 规则。尽管所有其他规则都运行良好。
  • 你的beforeValidate()有什么东西吗?
  • 这个问题刚刚更新了所有必要的信息
  • 你在用隐形墨水书写 =) 吗?没有查询日志(这可能是更重要的信息),并且.. 当它“不起作用”或“不起作用”实际上意味着什么时,您没有指出 $this->request->data 是什么。与您的直接问题无关的一件事-如果用户的密码在任何其他保存操作中,您将对其进行重新哈希处理。
  • 感谢您的评论。已添加保存用户的查询。

标签: cakephp


【解决方案1】:

在你的控制器中使用测试isUnique,看看它为什么不工作。 isUnique验证规则使用isUnique模型方法。

【讨论】:

    【解决方案2】:

    试试这个

      public $validate = array(                
          'email' => array(
            'rule1' => array(
                'rule' => 'email',
                'message' => 'Please enter a valid email address.'
            ),
            'rule2' => array(
                'rule' => 'isUnique',
                'message' => 'That email address exists.'
            )
        ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多