-1

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

模型

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

1 回答 1

3

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

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

于 2013-08-31T22:06:22.553 回答