Getting A Widget To Interact With The Database

Hi. I am trying to use Krajee’s star-rating widget to populate a field in my database. However, when I begin to use the ‘rating’ class in the code below, my sentimental_value field disappears from my POST altogether. If I am to delete the ‘class’ => ‘rating’ line, I can enter in a number in the text field, and it works fine. I just can’t get the needed information when using the widget.

Anybody have any ideas on the matter?




                            <?=

                            $form->field( $model, 'sentimental_value', [

                                'inputOptions' => [

                                    'class' => 'rating', 

                                    'type' => 'number',

                                    'input-id' => 'sentimental_value',

                                    'step'=>'1',

                                    'data-size'=>'sm',

                                    'data-show-clear'=>'false',

                                    'data-show-caption'=>'false',

                                ],

                            ] )->label( false );

                            ?>



There is a Yii2 StarRating widget created for this. You should be using that. Here is an updated code for your scenario:




<?= $form->field($model, 'sentimental_value')->widget(\kartik\widgets\StarRating::classname(), [

    'pluginOptions'=>[

        'step'=>1,

        'size'=>'sm',

        'showClear'=>false,

        'showCaption'=>false,

    ]

])->label( false );

?>



You can then receive the value of sentimental_value available in your model attributes, if you use $model->load(Yii::$app->request->post) method in your controller action. Note your attributes must be set to safe in your model validation for it to get value assigned during mass assignment.

Thanks for the help, Kartik. Your widgets seem excellent, and I look forward to using them.

I was able to get your StarRating widget to work by using your exact code, and I use the controller in the way that you described. However, in my POST, all of the values from the form are being passed to the object except for the sentimental_value. That field still isn’t being noticed as a POST field. Would you have any idea why?

As mentioned in my last part of the previous note - your attribute must be set to safe in your model validation rules.

Great! It ended up being a problem with my div blocks not allowing the widget to be visible, but other fields of the form were.

Thanks for the help!