<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>

CC BY-SA 3.0
2015-02-03T21:20:06.433Z
2015-02-03T21:20:06.433Z
Justin Kimbrell
<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>

CC BY-SA 3.0
2015-02-03T21:17:13.623Z
Possible dupe? https://craftcms.stackexchange.com/questions/4889/how-to-return-errors-from-a-model-back-to-the-template
Brad Bell
<plugin-development><error-message><error-templates><error>
Returning errors into templates
# 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 code example below
    {% if errors is defined %}
        
            {% for error in errors %}
                {{ error }}
            {% endfor %}
        
    {% 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() %}
    {{ error }}
{% endfor %}


    To Email
    
    {% if email.getError('toEmail') %}
    {{ email.getError('toEmail') }}
    {% endif %}


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'));
}


---

