Fixes #12080: Fixed afterValidate triggering when any validation occurs

This commit is contained in:
Ashton Schultz
2019-06-18 01:55:39 -07:00
committed by Alexander Makarov
parent 7832136107
commit 1f9131d841
3 changed files with 75 additions and 19 deletions

View File

@ -21,6 +21,7 @@ Yii Framework 2 Change Log
- Bug #17299: Fixed adding of input error class in `\yii\widgets\ActiveField::widget` (alexkart) - Bug #17299: Fixed adding of input error class in `\yii\widgets\ActiveField::widget` (alexkart)
- Bug #17328: Added mime aliases for BMP and SVG files (cmoeke) - Bug #17328: Added mime aliases for BMP and SVG files (cmoeke)
- Bug #17336: Fixed wildcard matching in Event::hasHandlers() (samdark) - Bug #17336: Fixed wildcard matching in Event::hasHandlers() (samdark)
- Bug #12080: Fixed afterValidate triggering when any validation occurs (czzplnm)
2.0.19 May 21, 2019 2.0.19 May 21, 2019

View File

@ -705,19 +705,20 @@
return false; return false;
} }
if (submitting) { var errorAttributes = [];
var errorAttributes = [], $input; var $input = findInput($form, this);
$.each(data.attributes, function () { $.each(data.attributes, function () {
$input = findInput($form, this); var hasError = (submitting && updateInput($form, this, messages)) || (!submitting && attrHasError($form, this, messages));
if (!$input.is(":disabled") && !this.cancelled && updateInput($form, this, messages)) {
if (!$input.is(":disabled") && !this.cancelled && hasError) {
errorAttributes.push(this); errorAttributes.push(this);
} }
}); });
$form.trigger(events.afterValidate, [messages, errorAttributes]); $form.trigger(events.afterValidate, [messages, errorAttributes]);
if (submitting) {
updateSummary($form, messages); updateSummary($form, messages);
if (errorAttributes.length) { if (errorAttributes.length) {
if (data.settings.scrollToError) { if (data.settings.scrollToError) {
var top = $form.find($.map(errorAttributes, function(attribute) { var top = $form.find($.map(errorAttributes, function(attribute) {
@ -787,7 +788,7 @@
var updateInput = function ($form, attribute, messages) { var updateInput = function ($form, attribute, messages) {
var data = $form.data('yiiActiveForm'), var data = $form.data('yiiActiveForm'),
$input = findInput($form, attribute), $input = findInput($form, attribute),
hasError = false; hasError = attrHasError($form, attribute, messages);
if (!$.isArray(messages[attribute.id])) { if (!$.isArray(messages[attribute.id])) {
messages[attribute.id] = []; messages[attribute.id] = [];
@ -795,7 +796,6 @@
attribute.status = 1; attribute.status = 1;
if ($input.length) { if ($input.length) {
hasError = messages[attribute.id].length > 0;
var $container = $form.find(attribute.container); var $container = $form.find(attribute.container);
var $error = $container.find(attribute.error); var $error = $container.find(attribute.error);
updateAriaInvalid($form, attribute, hasError); updateAriaInvalid($form, attribute, hasError);
@ -823,6 +823,28 @@
return hasError; return hasError;
}; };
/**
* Checks if a particular attribute has an error
* @param $form the form jQuery object
* @param attribute object the configuration for a particular attribute.
* @param messages array the validation error messages
* @return boolean whether there is a validation error for the specified attribute
*/
var attrHasError = function ($form, attribute, messages) {
var $input = findInput($form, attribute),
hasError = false;
if (!$.isArray(messages[attribute.id])) {
messages[attribute.id] = [];
}
if ($input.length) {
hasError = messages[attribute.id].length > 0;
}
return hasError;
};
/** /**
* Updates the error summary. * Updates the error summary.
* @param $form the form jQuery object * @param $form the form jQuery object

View File

@ -184,5 +184,38 @@ describe('yii.activeForm', function () {
assert.equal('New value', eventData.value); assert.equal('New value', eventData.value);
}); });
}); });
describe('afterValidate', function () {
var afterValidateSpy;
var eventData = null;
before(function () {
afterValidateSpy = sinon.spy(function (event, data) {
eventData = data;
});
});
after(function () {
afterValidateSpy.reset();
});
// https://github.com/yiisoft/yii2/issues/12080
it('afterValidate should trigger when not submitting', function () {
var inputId = 'name',
$input = $('#' + inputId);
$activeForm = $('#w0');
$activeForm.yiiActiveForm(
[{
"id": inputId,
"name": "name",
input: '#' + inputId
}], []).on('afterValidate', afterValidateSpy);
$activeForm.yiiActiveForm('validate');
assert.notEqual(null, eventData);
});
});
}); });
}); });