Uniform error messages

Hey there,

I am building my first form with the uniform plugin and came along a question I cannot answer. Is it possible to show only ONE message, when required fields are not filled? At the moment each field is giving me its own error, but I think it would be better to just mark the field and have a general warning like “Please fill out the required fields”.

Is this possible?

Thanks
Matthias

Yes, sure, you could adapt this snippet:

 <?php snippet('uniform/errors', ['form' => $form]) ?>

It currently looks like this:

<?php if (count($form->errors()) > 0): ?>
    <div class="uniform-errors">
        <?php foreach ($form->errors() as $key => $error): ?>
            <div class="uniform-errors__item">
                <?php echo implode('<br>', $error) ?>
            </div>
        <?php endforeach ?>
    </div>
<?php endif ?>

Instead of looping through all error messages, replace the loop with a single string:

<?php if (count($form->errors()) > 0): ?>
    <div class="uniform-errors">
        <p>Please fill out the required fields</p>
    </div>
<?php endif ?>

That only makes sense if all you care about is if the field is required or not. That message wouldn’t make sense, however, if you expect an email address and what the user entered doesn’t validate. Then just telling them that the field is required is not very helpful.

Oh, I haven’t thought about that. Thanks for the code.

How do you handle it? Showing the errors below each field or all together below or… ?

I used to display error messages next to the field, but according to what I read recently ("Form design patterns" by Adam Silver), it might be better to have an error summary at the top of the page (and also update the document title) with links to the actual fields.


(source: Form Design Patterns, p. 54)

1 Like

Thanks a lot for your help + input!