@tincap

Можно ли задать свои параметры в ActiveForm template?

$form = ActiveForm::begin([
        'id' => 'login-form',
        'fieldConfig' => [
            'template' => '{icon}{input}{error}'
        ],
    ]);


Можно ли добавить в template перменную "icon" таким способом? И где она потом определяется?

UPD: Похоже что только вот так https://stackoverflow.com/questions/23311242/yii2-...
  • Вопрос задан
  • 70 просмотров
Пригласить эксперта
Ответы на вопрос 2
kimono
@kimono
Web developer
Для этого, по всей видимости, вам нужно переопределить yii\widgets\ActiveField, а точнее методы в нём:
public $template = "{label}\n{icon}\n{input}\n{hint}\n{error}";
    public function render($content = null)
    {
        if ($content === null) {
            if (!isset($this->parts['{input}'])) {
                $this->textInput();
            }

            // добавить это
            if (!isset($this->parts['{icon}'])) {
                $this->icon();
            }

            if (!isset($this->parts['{label}'])) {
                $this->label();
            }
            if (!isset($this->parts['{error}'])) {
                $this->error();
            }
            if (!isset($this->parts['{hint}'])) {
                $this->hint(null);
            }
            $content = strtr($this->template, $this->parts);
        } elseif (!is_string($content)) {
            $content = call_user_func($content, $this);
        }

        return $this->begin() . "\n" . $content . "\n" . $this->end();
    }

public function icon($content, $options = [])
    {
        $this->parts['{icon}'] = Html::tag('i', null, ['class' => 'icon-'.$this->attribute];

        return $this;
    }

и использовать его в форме через:
$form = ActiveForm::begin([
  'fieldClass' => 'my\widgets\ActiveField'
]);
Ответ написан
Комментировать
@Arik
1.
"$icon{input}{error}"

2. перед инпутом
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы