Multiple sucess messages with multiple email actions in uniform plugin

I use uniform for building a contactform.
I configured three different email actions:

'_action' => 'email',
'to' => 'xxx@xxx',
'sender' => 'xxx@xxx',
'subject' => 'Subject1,
'service' => 'phpmailer',
],
'_action' => 'email',
// ...
'service' => 'phpmailer',
],
'_action' => 'email',
// ...
'service' => 'phpmailer',
],

If I successfully submit a form I get:
$form->echoMessage()
three times. Which means the text: “Thank you, the form was sent successfully.” is printed out more than once.

Do you have a question, like, how to avoid that?

Here is the answer: Uniform send custom reply

In the future, you would help us a lot if you ask a real question. Thank you.

Yes I want to avoid that. Thank you for the link :slight_smile:

I have the same issue. I followed this blog post : https://blog.the-inspired-ones.de/ajax-uniform

And added another action in the controller (sending a mail to another email address).

It works fine but when I successfully submit the form I get the “Thank you…” twice on the feedback line.

I checked your link but I have no idea how to solve it (php and javascript is not my cup of tea).

Should I put <?php $form->echoMessage(0) ?> somewhere ?

Many thanks for considering my request.

I think in your case, you should modify the code in contact-send.php:

<?php

header::contentType('application/json');
echo json_encode([
    'success' => $form->successful(),
    'message' => trim($form->message(0)),
    'errors' => array_keys(array_filter(get(), function ($field) use ($form) {
        return $form->hasError($field);
    }, ARRAY_FILTER_USE_KEY)),
]);

(not tested)

Thank you for your answer.

I already tried it, it works on the “Thank you…” messages, it only display one of them when the form is successfully submit.

The problem is, it doesn’t display the other messages, for example the error ones like “Please fill in all required fields.”.

That’s strange, because the error messages should not be affected by the message content. If I understand it correctly, message is just the success or failure message, whereas errors should return the error messages. I might be mistaken, though, @mzur?

Almost: message contains the success or error messages (including validation errors) and errors is a list of form field names for which the validation failed.

@Stella Once you pass an argument to the message function it will be restricted to the action, in this case the email action. But the email action does not perform validation, that’s done by Uniform. The (validation) messages of Uniform are stored under the special _uniform key in the messages.

Long story short, do this and you should get back your validation messages:

'message' => $form->message(0) ?: $form->message('_uniform'),

This works because the email action (0) is only executed if the validation was successful. If not, the message of the email action will be empty and the (validation) message of Uniform is returned instead. You can omit the trim with the newest version of Uniform.

1 Like

Thanks for clarifying, @mzur :slight_smile:

Many thanks to you both and your detailed explanation.

It works like a charm.