This forum is in read-only mode. Please continue to browse, but replying and other actions are disabled for now.

AlanQ
AlanQ

Summary

I would like to have custom error messages in an AJAX form.
Most especially, it would be good to have a custom message at the bottom of the form that simply says, "The form has not been sent, please correct the errors above". This would be particularly useful on small screen devices where the field errors might not be visible.

Background

Having read octobercms.com > docs > AJAX > Extra Features and watched Making Websites With October CMS - Part 25 - NEW! Ajax Form Validation I created an AJAX enabled form.

The form is opened with

Form::ajax('onSend', ['files' => true, 'flash' => true, 'validate' => true]);

I have the usual

<div data-validate-for="name" class="alert alert-danger"></div>

etc for each field.

At the bottom of the form is

<div class="alert alert-danger" data-validate-error><p data-message></p></div>

And the 'onSend' function contains the usual:

$validation = Validator::make($data, $rules);
if ($validation->fails()) {
    throw new ValidationException($validation);
}

This works, and I get the standard error messages.

Question 1

If I create an array of custom error messages $messages and add them to the validator make function:

$validation = Validator::make($data, $rules, $messages);

Validation still happens but I get the standard error messages; $messages is ignored.
Is this a bug in AJAX form validation?

Question 2

I can (partially) solve the problem in Question 1 by inserting custom messages directly in the form:

<div data-validate-for="name" class="alert alert-danger">CUSTOM ERROR MESSAGE</div>

Is there a way to have a custom error message at the form bottom?

This

<div class="alert alert-danger" data-validate-error><p data-message>CUSTOM ERROR MESSAGE</p></div>

doesn't work.

jyl4476029421
jyl4476029421
$messages = [
    'email_confirmation.*' => trans('my.beautiful.translatation.key'),
    'fieldname.rulename' => trans('my.plugin::lang.localized.key'),
    'phone_number.phone' => trans('system/validation.custom.phone_number.unique')
];
$validation = Validator::make($data, $rules, $messages);

Works just fine, you can find very complete documentation about this here: https://octobercms.com/docs/services/validation

1-2 of 2

You cannot edit posts or make replies: the forum is disabled.