D
D
Dmitry Kim2016-03-29 09:53:03
Yii
Dmitry Kim, 2016-03-29 09:53:03

How to return an ActiveForm from an anonymous function in YII2?

When rendering the GridView, it is necessary to display an "update" button along with a modal window with an ajax edit form.
To display the column I use the following code:

[
  'class'    => yii\grid\ActionColumn::className(),
  'template' => '{view} {update} {delete}',
  'buttons'  => [
    'update' => function ($url, $model, $key) {
        $html = [];
        $html[] = Modal::begin([
          'toggleButton' => ['label' => 'update', 'class' => 'btn btn-primary'],
        ]);
        $html[] = $form = ActiveForm::begin([
          'options'     => [
            'data-pjax' => true,
          ],
        ]);
        $html[] = $form->field($model, 'id')->textInput();
        // всякие поля формы
        $html[] = ActiveForm::end();
        $html[] = Modal::end();

        return implode("\n", $html);
    }
]

An error occurs: PHP Fatal Error - yii\base\ErrorException - Object of class yii\bootstrap\ActiveForm could not be converted to string.
How to correctly display the HTML code of the form from the function?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita, 2016-03-29
@kimono

I use ob_start()...etc.
A simple implementation, but a little bit unreadable, so I cover such comment blocks.

E
enchikiben, 2016-03-29
@EnChikiben

ActiveForm and Modal are descendants of Widget, which has a render() method that returns html, I would dig in this direction

M
Mylistryx, 2016-03-29
@Mylistryx

$this->render()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question