Conditional Field Attributes

What is the proper way of coding for conditional form field attributes?

I was trying to do something like

$bEditable = $model->isNewRecord || Yii::$app->user->can('Administrator');
echo $form->field(
	'maxlength' => true,
	$bEditable?null:'title'=>'This field is only editable by Administrators',

but it doesn’t work.

Can this be done in some fashion or do I have to create a If()else() and use multiple echos?

Not Tested but should work

$bEditable = $model->isNewRecord || Yii::$app->user->can('Administrator');
$options['maxlength'] = true;
if ($bEditable) {
    $options['disabled'] = 'disabled';
    $options['title'] = 'This field is only editable by Administrators';

echo $form->field($model, 'Company')->textInput($options);
1 Like

Very interesting! I would never have thought of that approach. Thank you for sharing. I will test it out and report back.

As always, thank you for taking the time to help!

Close but no cigar. It results in



0-maxlength="1" 0-disabled="disabled" 0-title="This field is only editable by Administrators"

Any other ideas?

Please disregard my last post!

Your code works perfectly, it was my mistake. Thank you once again.