yii2 Разрешнить редактирование одного поля формы, запретив все остальные (readonly)
Primary tabs
Проблема
В yii2 (на момент написание этой заметки) были в приоритет опция формы ('fieldConfig') над собственными опциям конктреного поля ('inputOptions' ). Всё дело было в порядке слияния конфигурации в array_merge().
Решение
Создаём собственный ActiveField как и раньше и добавляем (переопределяем) в него метод:
/** * Все как у родительского класса, но собственные настройки в приоритете * * Renders an input tag. * @param string $type the input type (e.g. `text`, `password`) * @param array $options the tag options in terms of name-value pairs. These will be rendered as * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. * * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. * * @return $this the field object itself. */ public function input($type, $options = []) { $options = array_merge($options, $this->inputOptions); // эта строка отличается от родительского класса $this->addAriaAttributes($options); $this->adjustLabelFor($options); $this->parts['{input}'] = Html::activeInput($type, $this->model, $this->attribute, $options); return $this; }
Теперь форму можно вызывать как-то так:
$form = ActiveForm::begin([ 'options' => ['class' => 'form'], 'fieldClass' => 'app\helpers\es\html\ActiveField', 'fieldConfig' => ['inputOptions' => ['readonly' => !(boolean)$parameters['alter'],
И отдельно переопределить readonly уже в конкретном поле -- например:
<?= $form->field($model, 'poisoning_site', ['template' => '', 'labelOptions' => ['class' => 'text-last'], 'inputOptions' => ['class' => '', 'readonly' => false]])->textarea() ?>
- Log in to post comments
- 5102 reads