From c59df914c1f544889eafc4d8aa64132ae1337102 Mon Sep 17 00:00:00 2001 From: Nikolay Date: Thu, 10 Jan 2019 17:17:37 +0300 Subject: [PATCH] Fixes #16253: Fixed empty checkboxlist validation --- framework/CHANGELOG.md | 1 + framework/assets/yii.activeForm.js | 51 +++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4758b37..e13c657 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.16 under development ------------------------ +- Bug #16253: Fixed empty checkboxlist validation (GHopperMSK) - Bug #15286: Fixed incorrect formatting of time with timezone information (rugabarbo) - Bug #17021: Fix to do not remove existing message category files in a subfolder (albertborsos) - Bug #16991: Removed usage of `utf8_encode()` from `Request::resolvePathInfo()` (GHopperMSK) diff --git a/framework/assets/yii.activeForm.js b/framework/assets/yii.activeForm.js index fbcece4..4f79b77 100644 --- a/framework/assets/yii.activeForm.js +++ b/framework/assets/yii.activeForm.js @@ -325,27 +325,39 @@ // client-side validation $.each(data.attributes, function () { this.$form = $form; - if (!findInput($form, this).is(":disabled")) { - this.cancelled = false; - // perform validation only if the form is being submitted or if an attribute is pending validation - if (data.submitting || this.status === 2 || this.status === 3) { - var msg = messages[this.id]; - if (msg === undefined) { - msg = []; - messages[this.id] = msg; + let $input = findInput($form, this); + + if ($input.is(":disabled")) { + return true; + } + // pass SELECT without options + if ($input.length && $input[0].tagName.toLowerCase() === 'select') { + if (!$input[0].options.length) { + return true; + } else if (($input[0].options.length === 1) && ($input[0].options[0].value === '')) { + return true; + } + } + this.cancelled = false; + // perform validation only if the form is being submitted or if an attribute is pending validation + if (data.submitting || this.status === 2 || this.status === 3) { + var msg = messages[this.id]; + if (msg === undefined) { + msg = []; + messages[this.id] = msg; + } + + var event = $.Event(events.beforeValidateAttribute); + $form.trigger(event, [this, msg, deferreds]); + if (event.result !== false) { + if (this.validate) { + this.validate(this, getValue($form, this), msg, deferreds, $form); } - var event = $.Event(events.beforeValidateAttribute); - $form.trigger(event, [this, msg, deferreds]); - if (event.result !== false) { - if (this.validate) { - this.validate(this, getValue($form, this), msg, deferreds, $form); - } - if (this.enableAjaxValidation) { - needAjaxValidation = true; - } - } else { - this.cancelled = true; + if (this.enableAjaxValidation) { + needAjaxValidation = true; } + } else { + this.cancelled = true; } } }); @@ -406,7 +418,6 @@ submitForm: function () { var $form = $(this), data = $form.data('yiiActiveForm'); - if (data.validated) { // Second submit's call (from validate/updateInputs) data.submitting = false;