phpyii2active-formform-fields

How to fix the "Getting unknown property" error when using form fields from a DB in Yii2?


I would like to implement a solution where the field types of a form are coming from the database.

My form is an ActiveForm with a model.

I was adding the following to the DB:

formfield: textInput

and this to the form:

<?= $form->field($model, 'rgw')->{$model->rrgw->formfield}(['maxlength' => true]) ?>

this is the only way it works, and only for textinput. As soon as I delete (['maxlength' => true]) it's not working anymore and I get the error:

Getting unknown property: yii\bootstrap\ActiveField::textInput

Furthermore, if I want a checkbox or something else, (['maxlength' => true]) is not wanted, right?

So I was trying to add () to the DB like this:

formfield: textInput()

I'm still getting error:

Getting unknown property: yii\bootstrap\ActiveField::textInput()

How can I get rid of the () part in Yii and move this to the DB?


Solution

  • textInput() and checkbox() are methods, so you need to use () to indicate that you want a methods instead of property.

    <?= $form->field($model, 'rgw')->{$model->rrgw->formfield}() ?>