cakePHP isUnique不再工作了

时间:2014-04-23 10:51:06

标签: cakephp

我有这个验证规则很长一段时间,它总是用来正常工作,但不再是。

  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'
                )
            )

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

以下是控制器的添加功能:

public function add(){

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')

2 个答案:

答案 0 :(得分:1)

通过在您的控制器中使用来测试isUnique以查看它无法正常工作的原因。 isUnique验证规则使用isUnique模型方法。

答案 1 :(得分:1)

试试这个

  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.'
        )
    ),