When a CSS class is passed as an array to yii\widgets\ActiveField, it throws an array to string error. Code sample below:
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Demo;
$model = new Demo();
$form = ActiveForm::begin();
$options = [];
Html::addCssClass($options, ['css-1','css-2']);
echo $form->field($model, 'email', ['options'=>$options])->textInput();
ActiveForm::end();
CSS classes should be allowed to be passed as an array to ActiveField or to Html:addCssClass
Array to string conversion error.
| Q | A
| ---------------- | ---
| Yii version | 2.0.13
| PHP version | Any
| Operating system | Any
This is incorrect usage as you attempt to pass HTML options to the field options, which are different things.
Input HTML options in your example should be passed to textInput() method:
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Demo;
$model = new Demo();
$form = ActiveForm::begin();
$options = [];
Html::addCssClass($options, ['css-1','css-2']);
echo $form->field($model, 'email')->textInput($options); // see the difference!
ActiveForm::end();
I am not sure you understood the issue and request to cross check again.
The problem is initiated in the ActiveForm::field method here which supports passing all ActiveField component properties as the third parameter to ActiveForm::field method.
Therefore, one should be able to set ActiveField::options using the above field method isn't it?
I know that one can pass options also via textInput($options) or other active field input methods to control the input styles... but if one needs to set FIELD CONTAINER styles... when setting ActiveField configuration as a parameter to the ActiveForm::field method as mentioned above, you will face an error as shown above... right? The issue is in the array parsing in this line of code.
Resolved by commit e69adc19a7473f219a9713404d8bbe017ed0c3ec
Most helpful comment
Resolved by commit e69adc19a7473f219a9713404d8bbe017ed0c3ec