Answers.Body: <p>If you use <code>$model-&gt;getErrors()</code> instead, it should return a multidimensional array of errors indexed by attribute name like so:</p>
  
  <pre><code>array(
      'email' =&gt; array(
          'First email error.',
          'Second email error.',
      ),
      'firstName' =&gt; array(
          'First firstName error.',
      ),
  );
  </code></pre>
  
  <p>Or you can get errors specifically for an attribute like so:</p>
  
  <pre><code>$model-&gt;getErrors('email');
  </code></pre>
  
  <p>Then update the template to check for the errors, since it is now a multidimensional array that's returned.</p>
  
  <pre><code>{% if errors.email is defined ? errors.email | length %}
  &lt;div data-alert class="alert"&gt;
      {% for error in errors.email %}
          {{ error }}
      {% endfor %}
  &lt;/div&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-22T18:53:44.547Z
Answers.Text: How would I call that in a template? {{ errors.name }} does not seem to work using `$model->getErrors()`.
Answers.UserDisplayName: Jason McCallister
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-22T20:40:47.840Z
Answers.Text: @JasonMcCallister If there were any errors on the `name` attribute, then `errors.name` will be an array of all of those errors.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-22T20:57:52.207Z
Answers.Text: Thanks Brandon, I updated brads answer to reflect that but it requires peer review to update.
Answers.UserDisplayName: Jason McCallister
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-22T21:52:17.973Z
Answers.Text: Approved (with minor tweaks)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-22T17:43:40.527Z
Answers.LastActivityDate: 2014-11-22T21:51:09.707Z
Answers.LastEditDate: 2014-11-22T21:51:09.707Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm working on validation with Craft. I have a custom plugin that allows public submission. However, I want to make sure I can pass errors back to the template. The key is trying to make my code as "dynamic" as possible. Meaning I do not want to specify the errors one by one.</p>
  
  <p>Here is my sample controller (keep in mind I don't care what the model says is required, trying to keep the code agnostic of the model.)</p>
  
  <pre><code>public function actionSave() {
  
      // require a post request
      $this-&gt;requirePostRequest();
  
      // assign specific fields we are listening for
      $post = craft()-&gt;request-&gt;getPost('fields');
  
      // create a new Request model
      $model = MyModel::populateModel($post);
  
      // validate the model attributes
      if ($model-&gt;validate()) {
          // save the record
  
          // redirect to posted url
      }
  
      $errors = $model-&gt;getAllErrors();
  
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'errors'    =&gt; $errors
      ));
  
  }
  </code></pre>
  
  <p>Using <code>$model-&gt;getAllErrors()</code> returns an array as listed below:</p>
  
  <pre><code>array
  (
      0 =&gt; 'Name cannot be blank.'
      1 =&gt; 'Details cannot be blank.'
      2 =&gt; 'Email is not a valid email address.'
  )
  </code></pre>
  
  <p>How would I return that array to specific fields in my template like so:</p>
  
  <pre><code>{% if errors.email is defined ? errors.email | length %}
      &lt;div data-alert class="alert"&gt;
          {{ errors.email }}
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-21T12:59:20.840Z
Comments.Text: What to use if no route is specified? how can I redirect back with error messages?
Comments.UserDisplayName: Volatil3
Tags: <plugin-development><controller><models><validation><error-templates>
Title: How to return errors from a model back to the template?
ThreadText: # How to return errors from a model back to the template?
  
  (Asked by: username_0 on 2014-11-22)
  
  I'm working on validation with Craft. I have a custom plugin that allows public submission. However, I want to make sure I can pass errors back to the template. The key is trying to make my code as "dynamic" as possible. Meaning I do not want to specify the errors one by one.
  
  Here is my sample controller (keep in mind I don't care what the model says is required, trying to keep the code agnostic of the model.)
  
  ```
  public function actionSave() {
  
      // require a post request
      $this->requirePostRequest();
  
      // assign specific fields we are listening for
      $post = craft()->request->getPost('fields');
  
      // create a new Request model
      $model = MyModel::populateModel($post);
  
      // validate the model attributes
      if ($model->validate()) {
          // save the record
  
          // redirect to posted url
      }
  
      $errors = $model->getAllErrors();
  
      craft()->urlManager->setRouteVariables(array(
          'errors'    => $errors
      ));
  
  }
  
  ```
  
  Using `$model->getAllErrors()` returns an array as listed below:
  
  ```
  array
  (
      0 => 'Name cannot be blank.'
      1 => 'Details cannot be blank.'
      2 => 'Email is not a valid email address.'
  )
  
  ```
  
  How would I return that array to specific fields in my template like so:
  
  ```
  {% if errors.email is defined ? errors.email | length %}
      <div data-alert class="alert">
          {{ errors.email }}
      </div>
  {% endif %}
  
  ```
  
  **Tags:** plugin-development, controller, models, validation, error-templates
  
  > **Comments:**
  > **username_1**: What to use if no route is specified? how can I redirect back with error messages?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2014-11-22. Score: 5 | Accepted answer
  
  If you use `$model->getErrors()` instead, it should return a multidimensional array of errors indexed by attribute name like so:
  
  ```
  array(
      'email' => array(
          'First email error.',
          'Second email error.',
      ),
      'firstName' => array(
          'First firstName error.',
      ),
  );
  
  ```
  
  Or you can get errors specifically for an attribute like so:
  
  ```
  $model->getErrors('email');
  
  ```
  
  Then update the template to check for the errors, since it is now a multidimensional array that's returned.
  
  ```
  {% if errors.email is defined ? errors.email | length %}
  <div data-alert class="alert">
      {% for error in errors.email %}
          {{ error }}
      {% endfor %}
  </div>
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: How would I call that in a template? {{ errors.name }} does not seem to work using `$model->getErrors()`.  
  > **username_3**: @username_0 If there were any errors on the `name` attribute, then `errors.name` will be an array of all of those errors.  
  > **username_0**: Thanks username_3, I updated brads answer to reflect that but it requires peer review to update.  
  > **username_4**: Approved (with minor tweaks)  
  
  ---
  
  