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