Yii 2: AJAX Validation not Active Form fields
-
There's Active Form with two fields:
$form->field($model)
- model field, throughActiveForm::validate($model)
The JSON is validated and sent with mistakes.Html::textInput()
-- not automatically validated
And the question is, we can easily check the field [2] and even return JSON in the same format and the same data from Active Form, but in the client area, they are not processed (the final version of the field [2] is fully in line with the field [1]). How to process a mistake and get a message?
There's a violin on the page.
yiiActiveForm
with the AF-field. Is it possible to add ours?Either comes to mind writing your JS script that will use API
yii.activeForm.js
Is it possible to add our fields to the JSON client processor with a set of mistakes?
-
If your own validation is:
ActiveForm::begin([ 'action' => '/process/save', 'validationUrl' => '/process/validation', ...
There's a way to put your processor on:
$('form').on('ajaxComplete', function (e, attribute, message) { if (attribute.responseJSON.delivery !== undefined){ // если ошибка то добавляем сообщение с ней $('.title').after('<div class="message-error"><p>'+attribute.responseJSON.delivery+'</p></div>'); } else if (attribute.responseJSON.delivery === undefined && $('.message-error').text().length > 0) { //если всё верно удаляем сообщение с ошибкой $('.message-error').remove(); }
});
On the server in
['/process/validation']
We' the field.if (!$Delivery->validate()) {
$this->_errors = ['delivery' => 'Ошибка валидации'];
throw new BadRequestHttpException();
}