I’m trying to set a custom message for a specific model attribute. I have something like this:

protected function defineAttributes() {
    return array(
        'attributeOne' => array(
            'required' => true,
            'label' => 'Attribute One Label'
        'attributeTwo' => array(
            'required' => true,
            'label' => 'Attribute Two Label'

How can I customise a specific error message for each attribute so that instead of ‘Attribute One Label cannot be blank’, I get ‘Attribute One Label with my custom message’?

3 Answers 3


If you use the static translation method, then that will override all {attribute} cannot be blank. translations, not just for your specific attribute.

Attribute validation error messages aren't directly tied to the attribute, but they are tied to a validator that is tied to an attribute.

I believe the proper way to do this would be to have a plugin that provides its own validator (even if it just extends one of Yii's default to change the message) and tell the attribute to use your custom validator.

You can see examples of how to approach that:




In addition to Brad’s answer (which is what I was looking for), you could also use template-specific messages using conditionals if for some reason you only needed to override the global error in a certain place. Unlikely but here’s an example:

{% if errors.attributeOne is defined ? errors.attributeOne | length %}
    <p class="alert">Custom message.</p>
{% endif %}
  • This is really handy thanks. I found that this also works straight out: {% if entry.getErrors('customField') is defined ? entry.getErrors('customField')|length %} <p class="alert">customField Custom message.</p> {% endif %} Mar 16, 2015 at 12:55

This might be similar to Brad Bell's recommendation, but if you are using Yii's built-in validation rules, you should just override the rules() method in your model or record, specify the rule there, and then customise the error message text for it.

For example:

 * Returns this record's validation rules.
 * @return array
public function rules()
    return array_merge(parent::rules(), [
        ['firstName2', 'compare', 'operator' => '!=', 'compareAttribute' => 'firstName1', 'message' => '{attribute} cannot be the same as {compareAttribute}']

Yii's documentation on rules shows the different parameters that can be set for each built-in rule/validator type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.