T
T
tincap2018-05-17 12:12:11
Yii
tincap, 2018-05-17 12:12:11

Can I set my own parameters in the ActiveForm template?

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

Is it possible to add "icon" variable to template in this way? And where is it then defined?
UPD: It seems that only like this https://stackoverflow.com/questions/23311242/yii2-...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kim, 2018-05-17
@kimono

To do this, apparently, you need to override yii\widgets\ActiveField, or rather the methods in it:

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;
    }

and use it in the form via:
$form = ActiveForm::begin([
  'fieldClass' => 'my\widgets\ActiveField'
]);

A
Arman, 2018-05-17
@Arik

1.
2. before input

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question