cakePHP唯一的电子邮件用户名

时间:2013-08-31 20:53:51

标签: cakephp

我只需要帮助识别用户的电子邮件,这也是数据库中的用户名,我在模型中使用了'isUnique',但由于某种原因,它没有给出错误消息,它仍然注册用户请给我一些帮助,这里是代码...

MODEL

App::uses('AuthComponent','Controller/Component');
class User extends AppModel {

     public $validate = array(
        'email' => 'email',
        'email' => array(
        'required' => array(
        'rule' => array('notEmpty'),
        'message' => 'Please enter a valid email address for username',
        'unique' => array(
        'rule' => 'isUnique',
        'message' => 'Please enter another email, this one is already taken'
       )
       )
       ),
        'password' => array(
        'required'=> array(
        'rule' => array('notEmpty'),
        'message' => 'Please enter a valid password',

        'rule' => array('minLength','8'),
        'message' => 'Please enter minimum 8 characters'
       )
       )
    );

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


 }
**CONTROLLER**

<?php


class usersController extends AppController
{
    public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add');
}
    var $name = 'Users';




    public function add()
     {

    if (!empty($this ->data))
    {
        $this->User->create();
        if ($this->User->save($this->data))
        {
            $this->Session->setFlash('Thank you for registering');
            $this->redirect(array('action'=>'index'));
        }
        else
        {
            // Make the password fields blank
            unset($this->data['User']['password']);
            unset($this->data['User']['confirm_password']);

            $this->Session->setFlash('An error occurred, try again!');
        }
    }


}

    function index()
    {

    }
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
      }
}

查看

<h2>End a problem registration</h2>
        <p>Please fill out details to register</p>

<?php
        echo $this->Form->Create('User',array('action' => 'add'));
        echo $this->Form->input('title');
        echo $this->Form->input('name');
        echo $this->Form->input('surname');
        echo $this->Form->input('email');
        echo $this->Form->input('password');
        echo $this->Form->input('password');
        echo $this->Form->end('Register');

1 个答案:

答案 0 :(得分:3)

您的验证规则的数组声明是错误的。 他们有错误的“级别”,从而使他们无效。 例如。电子邮件密钥使用了两次。

请根据文档更正它们 - 并使用正确的1标签缩进。 这将使它们既正确又易读,并且可以轻松防止您在上面犯下的错误。

相关问题