I'm trying to create a validation in the backend, of an email field that must have 1 or more emails, separated with the “;” character. To do this I was trying to use the following javascript inside a “Set Value” component:
Solution depends on the level of validation you need,
Obviously split in ";" returns an array so the length would indicate the number of components.
A simple check that each element contains the "@" character would be a simple check as to basic validity.
Past that i would be tempted to write a custom module/ formatter to do a fuller validation.
I was trying to follow your suggestion and created a custom formatter to do the validation, but it's not calling it in the validation. What am I doing wrong?
My input code looks like this:
<input type="text" class="form-control form-control-sm" id="inp_EMAIL" name="EMAIL" placeholder="Podem ser colocados vários endereços, separados por ponto e vírgula" required="" data-msg-required="Tem de indicar pelo menos um email" dmx-validate:custom_multiple_emails="inp_EMAIL.value.validateMultipleEmails()" data-msg-custom_multiple_emails="Um ou mais emails são inválidos. Use ';' para separar e verifique se não há emails vazios.">
Yes, that would be the format. The field in the database is a simple varchar field.
I wanted to validate it on the client (perhaps with a custom formatter, as Hyperbytes suggested) and on the server-side, but neither works
On your frontend form, you can add as many mails as you need, then a repeat on the server side: for each mail add it to the array, you can then use a set value so you can join them
I'll try to make an example later if you need, little busy right now
Here's my tip for you, first try to do the formatter server-side, forget all client-side validation. Only when server-side validation is working you can start on client-side. If you can't get server-side working I doubt you'll get client-side working either (server-side is easier in my opinion)
So, let's just stick with your Set Value, make a custom formatter and show your progress here
Are you using NodeJS or PHP? I'm assuming you're using NodeJS, I'm not familiar with PHP custom formatters
I managed to implement the server-side formatter and it already returns the error to the client. I just need to see how to put the error next to the input... many thanks for all the tips!
I managed to solve it with the server-side formatter, but this solution of using a tagify input on the client is very well thought out! I'm going to make a version of this and then I'll see which is better. Thanks for the tip.
function formatter_validateMultipleEmails($val) {
if ($val === null) {
return true; // Ou false, dependendo da sua lógica para null
}
$emailString = trim((string)$val);
if ($emailString === '') {
return true; // Campo vazio é válido por esta regra (required seria separado)
}
$emails = array_filter(array_map('trim', explode(';', $emailString)), function($email) {
return $email !== '';
});
if (empty($emails) && $emailString !== '') {
// Ex: o input foi só "; ; ;"
return false;
}
if (empty($emails)) return true; // Se era genuinamente vazio ou só delimitadores e vazio é ok
foreach ($emails as $email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
return false; // Pelo menos um email é inválido
}
}
return true; // Todos os emails são válidos
}
I ended up doing the best of both worlds: on the client I used @scalaris suggestion and put in a tagify. On the server-side, I continued to use formatter validation.
Thanks to everyone for their help and tips!