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 %}
        <ul class="errors">
            {% for error in errors %}
                <li>{{ error }}</li>
            {% endfor %}
    {% endif %}

1 Answer 1


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

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

The controller:

$model = new Postmaster_EmailFormModel(craft()->request->getPost());

    // Prepare a flash error message for the user.
    craft()->userSession->setError(Craft::t('The email could not be sent.'));

        'email' => $model
    $event = new Event($this, array(
        'email' => $model



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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