1

I can change form validator error message by these code in my model:

array('name, email, subject, body', 'required'
                        'message'=>'Please enter a value for {attribute}.'),

but i don't know where the {attribute} came from and how can i change it for each field, so any help would be appreciated.

3 Answers 3

4

Use these :

return array(
    // name, email, subject and body are required
    array('name', 'required',
                'message'=>'Please enter a value for name.'),
    array('email', 'required',
                'message'=>'Please enter a value for email.'),
    array('subject', 'required',
                'message'=>'Please enter a value for subject.'),
    array('body', 'required',
                'message'=>'Please enter a value for body.'),
Sign up to request clarification or add additional context in comments.

Comments

4

I'm not sure if i understand your question right, but you asked where the {attribute} came from:

Some validators introduce placeholders like the {attribute} in your example. If validation fails, they will be replaced with the attribute name. So if no name was entered and your message is 'Please enter a valid {attribute}.' the error message will be "Please enter a valid name".

While the {attribute} placeholder can be used with every validator, some of them introduce even more placeholders. For example with the CStringValidator you can use {min}, {max} or {length}. They will be replace with the number of minimum, maximum or exact characters respecitvely.

Here's an example:

array('firstname,lastname', 'string', 'min'=>3, 
    'tooShort'=>'Your {attribute} must contain at least {min} letters.'
),

This will give "Your firstname must contain at least 3 letters." if the users enters less than 3 letters. This has the advantage that if you change the min parameter, your message will automatically be updated. So it's less error prone.

Comments

2

The {attribute} is taken from your function:

    public function attributeLabels() {
    return array(
        'id' => 'ID',
        'name' => 'Name',
        'password' => 'Password',
        'email' => 'Email',

    );
}

on your model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.