RadioList - "required" validator problem


(Theoneandonly) #1

Hello everyone,

I am by no means an experienced Yii user, but I have done some smaller things in the past - and all of them worked well. (I am a software developer by trade, so I know how to program, how OOP works and all of this - I just don’t work with PHP and Yii much.)

Today I tried using the RadioList control for the first time, and it is driving me insane. The model’s attribute that is set by the RadioList is required - hence I defined it to be required in the model’s rules. But no matter what I do, validation always fails, both client and server side.

In my model there is




        public $num_times;


        public function rules()

        {

            return [

                        ['num_times', 'filter', 'filter' => 'strip_tags'],

                        ['num_times', 'trim'],

                        ['num_times', 'required', 'message' => 'This value is required'],

                ];

        }



It extends yii\base\Model and doesn’t contain much else except for the attribute label.

In my view, the skeleton is something I have used numerous times with other controls, so I think it is safe to only show how I create the RadioList:

My view:




<?= $form->field($model, 'num_times')->radioList([

    1 => 'Happened for the first time',

    2 => 'Happened for the second time',

    3 => 'Happened three times or more',

],['separator'=>'<br>'])->label(false); ?>



$model is populated with an instance of the model posted above by the controller before rendering the form - the same code with other models and other views works. In the controller I just copied a working block and changed the model class and view name. If I preset $model->num_times to a value of 1, 2 or 3 before rendering the view, the corresponding radio button is even selected when the view is rendered.

(The only difference from my code here to the real code are the strings for the option values and error message, as in reality everything is in German. I18N is not used as I need it to be in German only.)

The view is rendered fine, but as soon as I click on any of the three RadioButtons, the validator kicks in - and fails. I am totally clueless what is wrong.

Any help is highly appreciated!

Thank you from Germany

-Colaholiker


(Alirz23) #2

I think its your trim filter on client side.


// remove your trim filter

['num_times', 'trim'], 


// or disable client side validation, this goes in your view form options

'enableClientValidation' => false




(Softark) #3

I don’t see anything obviously wrong with your code, but probably one of the filters (‘strip_tags’ and ‘trim’) should be the cause of the problem — the selected value of ‘1’, ‘2’, or ‘3’ seems to be converted to ‘’ (an empty string) by the filter. I think you could trace the code with your debugger (in the server side and/or in the client side) and find out the bug.

Besides, I would not use ‘strip_tags’ filter or ‘trim’ filter for ‘num_times’.




//    ['num_times', 'filter', 'filter' => 'strip_tags'],

//    ['num_times', 'trim'],

    ['num_times', 'required', 'message' => 'This value is required'],

    ['num_times', 'in', 'range' => [1, 2, 3]],



http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#in


(Theoneandonly) #4

Thank you guys! It was in fact one of the two that caused trouble. I replaced it with an integer validator, limiting the range at the same time (so anything non-numeric, including HTML tags will be rejected anyway), and now it works! :D

Debugging this thing is a bit tricky - my development computer died and I need to do some urgent tweaks (through a SSH session) to a system that is live (with different Controllers of course, the one I am working on is not yet officially published and nothing links to it). So I can’t enable debug mode either…

But thanks to your support I fixed it! If I ever make it to South Africa or Japan, the next drink for you is on me! 8)


(Alirz23) #5

I am glad you got it working, I think I will file a bug report trim should not return empty string if the value is integer string.

cheers


(Theoneandonly) #6

You’re right. The official API docs say

In no serious programming language I have worked with so far, an integer is considered white space that can be trimmed away. And iirc PHP is the same in this aspect… ;D

Thanks for filing a bug report!


(Softark) #7

I also confirmed the bug. But it looks a bit complicated.




    ['attr', 'trim'],

    ['attr', 'required'],



The rules above will reproduce the bug in the client side validation when you use a radioList for the attribute.

But the following will not.




    ['attr', 'required'],

    ['attr', 'trim'],



And even the former will work as expected when you use a normal text input instead of a radioList.

Probably the error lies in the way that ‘trim’ client-side filter gets the value from a radioList.


(Timothy951) #8

Actually the problem is with radioButtonList container. It works only with “container=‘span’”.

Try your self by creating any radioButtonList and set it’s container=’’. Then this will not validate. Event this will not validate if you pass any tag except only “span”. mybkexperience