Skip to content

Commit

Permalink
Fix #8225: Fixed AJAX validation with checkboxList was only triggered…
Browse files Browse the repository at this point in the history
… on first select
  • Loading branch information
execut authored and samdark committed Oct 22, 2019
1 parent d7f69b6 commit 40fff67
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #17602: `EmailValidator` with `checkDNS=true` throws `ErrorException` on bad domains on Alpine (batyrmastyr)
- Enh #17607: Added Yii version 3 DI config compatibility (hiqsol)
- Bug #17606: Fix error in `AssetBundle` when a disabled bundle with custom init() was still published (onmotion)
- Bug #8225: Fixed AJAX validation with checkboxList was only triggered on first select (execut)
- Bug #17597: PostgreSQL 12 and partitioned tables support (batyrmastyr)
- Bug #17625: Fix boolean `data` attributes from subkeys rendering in `Html::renderTagAttributes()` (brandonkelly)

Expand Down
8 changes: 8 additions & 0 deletions framework/assets/yii.activeForm.js
Expand Up @@ -876,6 +876,14 @@
var type = $input.attr('type');
if (type === 'checkbox' || type === 'radio') {
var $realInput = $input.filter(':checked');
if ($realInput.length > 1) {
var values = [];
$realInput.each(function(index) {
values.push($($realInput.get(index)).val());
});
return values;
}

if (!$realInput.length) {
$realInput = $form.find('input[type=hidden][name="' + $input.attr('name') + '"]');
}
Expand Down
22 changes: 22 additions & 0 deletions tests/js/tests/yii.activeForm.test.js
Expand Up @@ -183,6 +183,28 @@ describe('yii.activeForm', function () {
$activeForm.yiiActiveForm('updateAttribute', inputId);
assert.equal('New value', eventData.value);
});

// https://github.com/yiisoft/yii2/issues/8225

it('the value of the checkboxes must be an array', function () {
var inputId = 'test_checkbox';
var $input = $('#' + inputId);

$activeForm = $('#w1');
$activeForm.yiiActiveForm('destroy');
$activeForm.yiiActiveForm([
{
id: inputId,
input: '#' + inputId
}
]).on('afterValidateAttribute', afterValidateAttributeSpy);

$input.find('input').prop('checked', true);
$activeForm.yiiActiveForm('updateAttribute', inputId);
var value = eventData.value;
assert.isArray(value);
assert.deepEqual(['1', '0'], value);
});
});

describe('afterValidate', function () {
Expand Down

0 comments on commit 40fff67

Please sign in to comment.