Answers.Body: <p>I just return the form model object back to the template. Yii has built in validation and error handling within the models themselves. In the example <code>email</code> is the instance of your form's model.</p>
  
  <p>The template:</p>
  
  <pre><code>{% if email is not defined %}
      {% set email = craft.postmaster.email() %}
  {% endif %}
  
  {% for error in email.getAllErrors() %}
      &lt;p&gt;{{ error }}&lt;/p&gt;
  {% endfor %}
  
  &lt;p&gt;
      &lt;label for="toEmail"&gt;To Email&lt;/label&gt;&lt;br&gt;
      &lt;input type="text" id="toEmail" name="toEmail" value="{{ email.toEmail }}" /&gt;
      {% if email.getError('toEmail') %}
      &lt;label for="toEmail"&gt;{{ email.getError('toEmail') }}&lt;/label&gt;
      {% endif %}
  &lt;/p&gt;
  </code></pre>
  
  <p>The controller:</p>
  
  <pre><code>$model = new Postmaster_EmailFormModel(craft()-&gt;request-&gt;getPost());
  
  if(!$model-&gt;validate())
  { 
      // Prepare a flash error message for the user.
      craft()-&gt;userSession-&gt;setError(Craft::t('The email could not be sent.'));
  
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'email' =&gt; $model
      ));
  }
  else
  {
      $event = new Event($this, array(
          'email' =&gt; $model
      ));
  
      craft()-&gt;postmaster_forms-&gt;onEmailFormSend($event);
  
      $this-&gt;redirect(craft()-&gt;request-&gt;getPost('redirect'));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T21:20:06.433Z
Answers.LastActivityDate: 2015-02-03T21:20:06.433Z
Answers.UserDisplayName: Justin Kimbrell
Body: <p>When working with a custom form that posts to a plugin function - what's the best way to return errors that would be displayed like the <a href="http://buildwithcraft.com/docs/templating/forgot-password-form" rel="nofollow">forgot password</a> code example below</p>
  
  <pre><code>    {% if errors is defined %}
          &lt;ul class="errors"&gt;
              {% for error in errors %}
                  &lt;li&gt;{{ error }}&lt;/li&gt;
              {% endfor %}
          &lt;/ul&gt;
      {% endif %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-03T21:17:13.623Z
Comments.Text: Possible dupe? https://craftcms.stackexchange.com/questions/4889/how-to-return-errors-from-a-model-back-to-the-template
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><error-message><error-templates><error>
Title: Returning errors into templates
ThreadText: # Returning errors into templates
  
  (Asked by: username_0 on 2015-02-03)
  
  When working with a custom form that posts to a plugin function - what's the best way to return errors that would be displayed like the [forgot password](http://buildwithcraft.com/docs/templating/forgot-password-form) code example below
  
  ```
      {% if errors is defined %}
          <ul class="errors">
              {% for error in errors %}
                  <li>{{ error }}</li>
              {% endfor %}
          </ul>
      {% endif %}
  
  ```
  
  **Tags:** plugin-development, error-message, error-templates, error
  
  > **Comments:**
  > **username_1**: Possible dupe? https://craftcms.stackexchange.com/questions/4889/how-to-return-errors-from-a-model-back-to-the-template  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-02-03. Score: 4 | Accepted answer
  
  I just return the form model object back to the template. Yii has built in validation and error handling within the models themselves. In the example `email` is the instance of your form's model.
  
  The template:
  
  ```
  {% if email is not defined %}
      {% set email = craft.postmaster.email() %}
  {% endif %}
  
  {% for error in email.getAllErrors() %}
      <p>{{ error }}</p>
  {% endfor %}
  
  <p>
      <label for="toEmail">To Email</label><br>
      <input type="text" id="toEmail" name="toEmail" value="{{ email.toEmail }}" />
      {% if email.getError('toEmail') %}
      <label for="toEmail">{{ email.getError('toEmail') }}</label>
      {% endif %}
  </p>
  
  ```
  
  The controller:
  
  ```
  $model = new Postmaster_EmailFormModel(craft()->request->getPost());
  
  if(!$model->validate())
  { 
      // Prepare a flash error message for the user.
      craft()->userSession->setError(Craft::t('The email could not be sent.'));
  
      craft()->urlManager->setRouteVariables(array(
          'email' => $model
      ));
  }
  else
  {
      $event = new Event($this, array(
          'email' => $model
      ));
  
      craft()->postmaster_forms->onEmailFormSend($event);
  
      $this->redirect(craft()->request->getPost('redirect'));
  }
  
  ```
  
  
  ---
  
  