<p>I've done something similar recently... The errors returned by that macro are errors on the <code>user</code> model. Therefore, rather than creating errors on the user session, why not create them on the user model during registration and they'll automatically be available in the same macro...</p>

<p>Here's an example I'm using in a custom module to add extra validation to the user registration form:</p>

<pre><code>// --------------------------------------------
// BEFORE SAVE USER ELEMENT EVENT. Add our own validation
// --------------------------------------------

use craft\elements\User as UserElement;
use craft\base\Element;
use craft\events\ModelEvent;

Event::on(
    UserElement::class,
    Element::EVENT_BEFORE_SAVE,
    function(ModelEvent $e) {
        if ($e-&gt;isNew &amp;&amp; $e-&gt;sender instanceof \craft\elements\User) {
            $errors = false;
            if (!Craft::$app-&gt;request-&gt;post('firstName')){
                $e-&gt;sender-&gt;addError('firstName','First name is required');
                $errors = true;
            }
            if (!Craft::$app-&gt;request-&gt;post('lastName')){
                $e-&gt;sender-&gt;addError('lastName','Last name is required');
                $errors = true;
            }
            if (strlen(Craft::$app-&gt;request-&gt;post('fields.phoneNumber')) &lt; 10) {
                $e-&gt;sender-&gt;addError('phoneNumber','Telephone number should be at least 10 characters long');
                $errors = true;
            }
            if (!is_numeric(Craft::$app-&gt;request-&gt;post('fields.phoneNumber'))) {
                $e-&gt;sender-&gt;addError('phoneNumber','Telephone number must be numeric');
                $errors = true;
            }
            if ($errors){
                $e-&gt;isValid = false;
            }
        }
    }
);
</code></pre>

<p>Then in your template you'd have something like this after every field (I tend to prefer includes over macros but macros would work fine too):</p>

<pre><code>...
&lt;li class="{{ user is defined and user.getFirstError('phoneNumber') ? 'has-error' }}"&gt;
    &lt;label for="phoneNumber"&gt;Telephone number &lt;em&gt;&amp;bull;&lt;/em&gt;&lt;/label&gt;
    {# minlength is not supported in IE11 so add a server side check too #}
    &lt;input minlength="10" required id="phoneNumber" type="tel" name="fields[phoneNumber]" value="{{ user is defined ? user.phoneNumber }}"&gt;
    {% if user is defined and user.getFirstError('phoneNumber') %}
        {% include '_partials/fieldErrors.twig' with { errors: user.getErrors('phoneNumber') } %}
    {% endif %}
&lt;/li&gt;
...
</code></pre>

<pre><code>### _partials/fieldErrors.twig:

{% if errors %}
    &lt;ul&gt;
        {% for error in errors %}
            &lt;li&gt;{{ error }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

CC BY-SA 4.0
2020-03-03T16:43:17.337Z
2020-03-03T16:54:08.443Z
2020-03-03T16:54:08.443Z
James Smith
<p>Using CraftCMS example registration form in the docs.</p>

<p>How would I add a new error message to the macro <code>errorList</code> in the registration form:</p>

<pre><code>    {% macro errorList(errors) %}
        {% if errors %}

                {% for error in errors %}
                    &lt;span&gt;{{ error }}&lt;/span&gt;
                {% endfor %}

        {% endif %}
    {% endmacro %}

    {# Output if true #}

    {% if user is defined %}
        {{ _self.errorList(user.getErrors('email')) }}
    {% endif %}

</code></pre>

<p>I added a checkbox and need to create a validation for it. I can create an error like this: <code>Craft::$app-&gt;getSession()-&gt;setError(Craft::t('app', 'Check box is not checked'));</code> which works, but how will I add an error to the macro <code>errorList</code> so I can use it the same as the other errors eg, firstname, password, etc.</p>

<p>CraftCMS: 3.4.6.1</p>

<plugin-development>
Add new error to macro errorList - User Registration
# Add new error to macro errorList - User Registration

(Asked by: username_0 on 2020-03-03)

Using CraftCMS example registration form in the docs.
How would I add a new error message to the macro errorList in the registration form:
    {% macro errorList(errors) %}
        {% if errors %}

                {% for error in errors %}
                    {{ error }}
                {% endfor %}

        {% endif %}
    {% endmacro %}

    {# Output if true #}

    {% if user is defined %}
        {{ _self.errorList(user.getErrors('email')) }}
    {% endif %}


I added a checkbox and need to create a validation for it. I can create an error like this: Craft::$app->getSession()->setError(Craft::t('app', 'Check box is not checked')); which works, but how will I add an error to the macro errorList so I can use it the same as the other errors eg, firstname, password, etc.
CraftCMS: 3.4.6.1

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2020-03-03. Score: 2 | Accepted answer

I've done something similar recently... The errors returned by that macro are errors on the user model. Therefore, rather than creating errors on the user session, why not create them on the user model during registration and they'll automatically be available in the same macro...
Here's an example I'm using in a custom module to add extra validation to the user registration form:
// --------------------------------------------
// BEFORE SAVE USER ELEMENT EVENT. Add our own validation
// --------------------------------------------

use craft\elements\User as UserElement;
use craft\base\Element;
use craft\events\ModelEvent;

Event::on(
    UserElement::class,
    Element::EVENT_BEFORE_SAVE,
    function(ModelEvent $e) {
        if ($e->isNew && $e->sender instanceof \craft\elements\User) {
            $errors = false;
            if (!Craft::$app->request->post('firstName')){
                $e->sender->addError('firstName','First name is required');
                $errors = true;
            }
            if (!Craft::$app->request->post('lastName')){
                $e->sender->addError('lastName','Last name is required');
                $errors = true;
            }
            if (strlen(Craft::$app->request->post('fields.phoneNumber')) < 10) {
                $e->sender->addError('phoneNumber','Telephone number should be at least 10 characters long');
                $errors = true;
            }
            if (!is_numeric(Craft::$app->request->post('fields.phoneNumber'))) {
                $e->sender->addError('phoneNumber','Telephone number must be numeric');
                $errors = true;
            }
            if ($errors){
                $e->isValid = false;
            }
        }
    }
);

Then in your template you'd have something like this after every field (I tend to prefer includes over macros but macros would work fine too):
...

    Telephone number •
    {# minlength is not supported in IE11 so add a server side check too #}
    
    {% if user is defined and user.getFirstError('phoneNumber') %}
        {% include '_partials/fieldErrors.twig' with { errors: user.getErrors('phoneNumber') } %}
    {% endif %}

...

### _partials/fieldErrors.twig:

{% if errors %}
    
        {% for error in errors %}
            {{ error }}
        {% endfor %}
    
{% endif %}


---

