1

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 %}
                    <span>{{ error }}</span>
                {% 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

1 Answer 1

2

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

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

{% if errors %}
    <ul>
        {% for error in errors %}
            <li>{{ error }}</li>
        {% endfor %}
    </ul>
{% endif %}

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.