Uniform error messages

#1

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

0 Likes

#2

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.

0 Likes

#3

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

0 Likes

#4

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

#5

Thanks a lot for your help + input!

0 Likes