On Dec 18, 2010, at 04:36, John Maxim wrote:
> On Dec 18, 6:29 pm, John Maxim wrote:
>> 
>> It works with the same array key 'email' ? I tried, it works. May I
>> know why  ?
> 
> I think I understand now. Is it because the first name is the array
> name, and the following is the first array key ? therefore, subsequent
> is second key....and on...  ?

I didn't understand your question -- didn't know which "email" you were 
referring to since "email" occurs several times in your array. I agree it is a 
bit confusing the way CakePHP uses a deeply-nested array to set up validation, 
because you have to understand what each little bit is for. It took me awhile 
to work through the documentation and examples on the topic. I can try to 
explain it line by line in your example. Again I'll reformat it slightly and 
set "last" to true in each:


var $validation = array(
    '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.',
            'last' => true,
        ),
    ),
);


Looking at each line:


> var $validation = array(

Defines the validation array. Each entry in this array is for one field that we 
want to validate.

>     'email' => array(


Here we define the validation rules for the field named "email". Each entry in 
this array is one validation rule.

>         'notEmpty' => array(

We're calling the first validation rule "notEmpty". This string has no 
significance whatsoever to CakePHP; it's just a name to help you remember what 
the rule is about, and needs to be unique for this field.

>             'rule' => 'notEmpty',

This validation rule uses the CakePHP built-in rule "notEmpty" which ensures 
the field is not empty.

>             'message' => 'This field cannot be blank',

If the validation rule does not pass (i.e. if the field is empty), this message 
will be shown.

>             'last' => true,

If validation does not pass, no further rules will be processed.

>         ),
>         'email' => array(

This is the second validation rule for this field; we're calling this second 
rule "email", but again, that name has no significance.

>             'rule' => 'email',

We're asking CakePHP to use its built-in rule "email" which verifies the field 
contains a correctly-formatted email address.

>             'message' => 'That is not a valid email address.',

If validation fails, this message is shown.

>             'last' => true,

If validation fails, no further rules are processed. Somewhat irrelevant here 
since this is the last rule for this field anyway, but I like to include it 
always, since it makes it easier to add further rules later.

>         ),
>     ),
> );



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