Answers.Body: <p>If you use the <a href="http://buildwithcraft.com/help/static-translations" rel="nofollow">static translation</a> method, then that will override <em>all</em> <code>{attribute} cannot be blank.</code> translations, not just for your specific attribute.</p>
  
  <p>Attribute validation error messages aren't directly tied to the attribute, but they are tied to a <a href="http://www.yiiframework.com/doc/api/1.1/CValidator" rel="nofollow">validator</a> that is tied to an attribute.</p>
  
  <p>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.</p>
  
  <p>You can see examples of how to approach that:</p>
  
  <p><a href="http://www.yiiframework.com/wiki/56/" rel="nofollow">http://www.yiiframework.com/wiki/56/</a></p>
  
  <p><a href="http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/" rel="nofollow">http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-22T01:05:30.943Z
Answers.LastActivityDate: 2015-02-22T01:05:30.943Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>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 <code>rules()</code> method in your model or record, specify the rule there, and then customise the error message text for it. </p>
  
  <p>For example:</p>
  
  <pre><code>/**
   * Returns this record's validation rules.
   *
   * @return array
   */
  public function rules()
  {
      return array_merge(parent::rules(), [
          ['firstName2', 'compare', 'operator' =&gt; '!=', 'compareAttribute' =&gt; 'firstName1', 'message' =&gt; '{attribute} cannot be the same as {compareAttribute}']
      ]);
  }
  </code></pre>
  
  <p><a href="http://www.yiiframework.com/wiki/56/" rel="nofollow">Yii's documentation on rules</a> shows the different parameters that can be set for each built-in rule/validator type.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-01T04:29:39.343Z
Answers.LastActivityDate: 2016-04-01T04:29:39.343Z
Answers.UserDisplayName: Tim Everts
Answers.Body: <p>In addition to Brad&#8217;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:</p>
  
  <pre><code>{% if errors.attributeOne is defined ? errors.attributeOne | length %}
      &lt;p class="alert"&gt;Custom message.&lt;/p&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-16T12:55:17.747Z
Answers.Text: 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 %}`
Answers.UserDisplayName: Simon Clayson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-22T13:59:03.113Z
Answers.LastActivityDate: 2015-02-22T15:27:48.933Z
Answers.LastEditDate: 2015-02-22T15:27:48.933Z
Answers.UserDisplayName: James
Body: <p>I’m trying to set a custom message for a specific model attribute. I have something like this:</p>
  
  <pre><code>protected function defineAttributes() {
      return array(
          'attributeOne' =&gt; array(
              AttributeType::String,
              'required' =&gt; true,
              'label' =&gt; 'Attribute One Label'
          ),
          'attributeTwo' =&gt; array(
              AttributeType::String,
              'required' =&gt; true,
              'label' =&gt; 'Attribute Two Label'
          )
      );
  }
  </code></pre>
  
  <p>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’?</p>
  
Tags: <plugin-development><models><validation>
Title: Customise errors when validating model data?
ThreadText: # Customise errors when validating model data?
  
  (Asked by: username_0 on 2015-02-21)
  
  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(
              AttributeType::String,
              'required' => true,
              'label' => 'Attribute One Label'
          ),
          'attributeTwo' => array(
              AttributeType::String,
              '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’?
  
  **Tags:** plugin-development, models, validation
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2015-02-22. Score: 1 | Accepted answer
  
  If you use the [static translation](http://buildwithcraft.com/help/static-translations) 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](http://www.yiiframework.com/doc/api/1.1/CValidator) 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:
  
  <http://www.yiiframework.com/wiki/56/>
  
  <http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/>
  
  
  ---
  
  ### Answer by username_2 on 2016-04-01. Score: 2
  
  This might be similar to username_1 username_1'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](http://www.yiiframework.com/wiki/56/) shows the different parameters that can be set for each built-in rule/validator type.
  
  
  ---
  
  ### Answer by username_0 on 2015-02-22. Score: 2
  
  In addition to username_1’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 %}
  
  ```
  
  > **Comments:**
  > **username_3**: 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 %}`  
  
  ---
  
  