Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yii.activeForm.js getValue error for checkbox #16916

Open
buptlsp opened this issue Nov 27, 2018 · 1 comment
Open

yii.activeForm.js getValue error for checkbox #16916

buptlsp opened this issue Nov 27, 2018 · 1 comment
Labels
JS JavaScript related

Comments

@buptlsp
Copy link

buptlsp commented Nov 27, 2018

Hello, I have write a DictValidator for yii2, so I Inject a dict() function in yii.validation.js and so it can check the value is ok. it work well on any other input except the checkbox, so I review the yii.activeForm.js, and i find the bug.

this is now code of function yii.activeForm.js:

   var getValue = function ($form, attribute) {
         var $input = findInput($form, attribute);
         var type = $input.attr('type');
         if (type === 'checkbox' || type === 'radio') {
             var $realInput = $input.filter(':checked');
             if (!$realInput.length) {
                 $realInput = $form.find('input[type=hidden][name="' + $input.attr('name') + '"]');
             }

             return $realInput.val();
         } else {
             return $input.val();
         }
     };

if type == 'checkbox', we may expect the function return an array of value, but it return a string。this is an example:
step1:
I put a checkbox group on the page, and set a js block
image
image

step 2 : I click the button, and see the result:
the getValue should return ["上海", "广州"], but the return is "上海"
image

I fixed the code like this, and it works well:

     var getValue = function ($form, attribute) {
         var $input = findInput($form, attribute);
         var type = $input.attr('type');
         if (type === 'checkbox' || type === 'radio') {
             var $realInput = $input.filter(':checked');
             if (!$realInput.length) {
                 $realInput = $form.find('input[type=hidden][name="' + $input.attr('name') + '"]');
             }
             if(type === 'checkbox') {
                 var ret = [];
                 $realInput.each(function(){
                      ret.push($(this).val());
                 });
                 return ret;
             }
             return $realInput.val();
         } else {
             return $input.val();
         }
     };

Additional info

Q A
Yii version 2.0.15
PHP version 7.1.20
Operating system centos
@samdark samdark added the JS JavaScript related label Nov 28, 2018
@np25071984
Copy link
Contributor

Hi. Can you show click event handler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS JavaScript related
Projects
None yet
Development

No branches or pull requests

3 participants