开发者

CakePHP validation, error messages in Auth and disabling modified.

I have tried to get this function working in the desired way but not having much luck. The good news is, is that it works. Only thing I wanted to get working correctly is the field validation error messages which I managed to, but could not login in even if the username and password were correct. I have rolled back the function in the hope that someone could shed some light on this for me please.

    function login() {
            $this->layout = 'login';
            $this->set('title_for_layout', 'Login');
            if (!empty($this->data) && $id = $this->Auth->user('id')) { 
                    $this->User->id = $id; 
                    $this->User->saveField('last_login', date('Y-m-d H:i:s')); 
                    $this->Session->setFlash(__('You have successfully logged in', true));
                    $this->redirect(array('action'=>'index')); 
            }    
    }

The validation which I have setup in the users model:

var $validate = array(
                'username' => array(
               ),
                'login' => array(
                            'rule' => 'isUnique',
                            'message' => 'This username has already been taken',
                     ),
                    'pattern' => array(
                            'rule' => array('custom','/[a-zA-Z0-9\_\-]{6,}$/i'),
                            'message' => 'Must be 6 characters or longer with no spaces',
                    ),
                    'length' => array(
                            'rule' => array('maxLength', 15),
                            'message' => 'Please keep username under 15 characters',
                    ),
                    'notempty' => array(
                            'rule' => array('notempty'),
                            'message' => 'Username cannot be empty',
                    ),
            ),
        'password' => array(
                        'notempty' => array(
        开发者_Python百科                        'rule' => array('notempty'),
                                'message' => 'Password cannot be empty',
                        ),
                        'length' => array(
                                'rule' => array('between', 5, 15),
                                'message' => 'Passwords must be between 5 and 15 characters long with no spaces',
                                'on' => 'create',
                        )
        )
);

I shall unset certain validation rules for the login, I for now would just like to get the validation actually working in the correct manner.

Really, I have three questions...

  1. Why is my validation not working correctly?

  2. How can I display messages from the AuthComponent in my views?

  3. How can I temporarily disable the modified field in my table from updating?

Many thanks!


Beside the error I found in my comment I'm fairly certain your login validation isn't setup correctly.

Here's what you have:

'login' => array(
                'rule' => 'isUnique',
                'message' => 'This username has already been taken',
            ),
                        'pattern' => array(
                                'rule' => array('custom','/[a-zA-Z0-9\_\-]{6,}$/i'),
                                'message' => 'Must be 6 characters or longer with no spaces',
                        ),
                        'length' => array(
                                'rule' => array('maxLength', 15),
                                'message' => 'Please keep username under 15 characters',
                        ),
                        'notempty' => array(
                                'rule' => array('notempty'),
                                'message' => 'Username cannot be empty',
                        ),
        ),

Here's what I think you want to have

'username' => array(
    'isUnique' => array(
        'rule' => 'isUnique',
        'message' => 'This username has already been taken'
    ),
    'pattern' => array(
        'rule' => array('custom','/[a-zA-Z0-9\_\-]{6,}$/i'),
        'message' => 'Must be 6 characters or longer with no spaces',
    ),
    'length' => array(
        'rule' => array('maxLength', 15),
        'message' => 'Please keep username under 15 characters',
    ),
    'notEmpty' => array(
        'rule' => 'notEmpty',
        'message' => 'Username cannot be empty',
    ),
),

Mandatory links to the CakePHP Book and the CakePHP API.


Answer to error from OP in comment:

I see now what you were trying to do. Try this:

$id = $this->Auth->user('id');
if (!empty($this->data) && $id) { 
                $this->User->id = $id;


Answer to "How to display my login error message from the AuthComponent?"

For the login error message to appear you will need to add the following line in your layout or view for your login form.

echo $this->Session->flash('auth');


Answer to "How to temporarily disable CakePHP's modified behavior?":

Well, a brief look at the source code and the answer to this question on Cake's internal QA you can add something like this before you save the data.

$this->data['User']['modified'] = false;

This tells Cake to not update the modified field.

Interestingly enough from the source code it appears that you could just as easily replace false with true and it wouldn't update the field. I don't know if this is actually the case though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜