Taking validation further
(TUTORIAL)
Sometimes you can't tell if data is valid just by looking at it. For
example, the username may be between six and 40 characters, but you
will have to check the database to see if the username is already
taken. CakePHP provides the ability to manually mark a field as
invalid. Take a look at the beforeValidate method in Listing 23. This
method would be added to the user model.

Listing 23. Validate the username


 function beforeValidate() {
    if (!$this->id) {
        if ($this->findCount(array('User.username'
                                => $this->data['User']['username'])) > 0) {
            $this->invalidate('username_unique');
            return false;
        }
    }
    return true;
}


+++++++++++++++++++++QUESTIONER++++++++++++++++++++++
So we need to put this in user model ? but currently there isn't any
function in it.. how do I add it in ?

Before posting for help here, I have tried adding in and it all went
wrong. Now I would like to find out where exactly to add this and is
this the phpcake 3.1 version's script ? I noticed many scripts were
deprecated and I have to keep checking on the comments.

My current user model script is as follow:

<?php

class User extends AppModel
        {
                var $name = 'User';

                var $validate = array (
                'username' => array ('rule' => '/^.{6,40}$/'),

                'password' => array('rule'=>'/^.{6,40}$/'),

                'email' => array(
                'notEmpty' => array(
                        'rule' => 'notEmpty',
                        'message' => 'This field cannot be blank',
                        'last' => true
                ),
                'email' => array(
                        'rule' => 'email',
                        'message' => 'That is not a valid email
address'
                )
            )

                );

        }

?>

(TUTORIAL CONTINUE)
Is Listing 24 compulsory ? :

You can take full advantage of this by changing the username input
line in the register.ctp view to the following.

Listing 24. New username input line

 echo $form->input('username', array('after' => $form->error
       ('username_unique', 'The username is taken. Please try
again.')));

+++QUESTIONER+++
I really have no idea what this can do, lost. I have put both Listing
23 & 24 in but it gave a list of 4-5 errors



Any help please, Thanks !

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to