DBCreator
(DBCreator)
1
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(
$model,
'Company'
)->textInput([
'maxlength' => true,
$bEditable?null:'disabled'=>'disabled',
$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
DBCreator
(DBCreator)
3
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!
DBCreator
(DBCreator)
4
Close but no cigar. It results in
0-maxlength="1"
and/or
0-maxlength="1" 0-disabled="disabled" 0-title="This field is only editable by Administrators"
Any other ideas?
DBCreator
(DBCreator)
5
Please disregard my last post!
Your code works perfectly, it was my mistake. Thank you once again.