Yii2: ActiveForm - EVENT_AFTER_RUN

Created on 17 Jul 2018  ·  11Comments  ·  Source: yiisoft/yii2

Событие EVENT_AFTER_RUN для yiiwidgetsActiveForm задано неверно, я ожидаю контент в $event->result, но в итоге там пусто.

Вот бехейвер, который подключается к форме, в нем фукнция на событие EVENT_AFTER_RUN

class MyBehavior extends Behavior
{
    public function events()
    {
        return [
            Widget::EVENT_AFTER_RUN => 'myFunction',
        ];
    }

    public function myFunction($event)
    {
        var_dump($event->result); // null
    }
}

Не срабатывает потому, что в функции yiiwidgetsActiveForm::run() контент сразу выводится, а не возвращается

    public function run()
    {
        if (!empty($this->_fields)) {
            throw new InvalidCallException('Each beginField() should have a matching endField() call.');
        }

        $content = ob_get_clean();
        echo Html::beginForm($this->action, $this->method, $this->options);
        echo $content;

        if ($this->enableClientScript) {
            $this->registerClientScript();
        }

        echo Html::endForm();
    }

Если переработать функцию, то все работает

    public function run()
    {
        if (!empty($this->_fields)) {
            throw new InvalidCallException('Each beginField() should have a matching endField() call.');
        }

        $content = ob_get_clean();

        $html = Html::beginForm($this->action, $this->method, $this->options);
        $html .= $content;

        if ($this->enableClientScript) {
            $this->registerClientScript();
        }

        $html .= Html::endForm();
        return $html;
    }

| Q | A
| ---------------- | ---
| Yii version | 2.0.15.1.
| PHP version | 7.1.8
| Operating system | win10

ready for adoption bug

All 11 comments

Pull request сделаете?

For what you did this? Now need to print the form explicitly? Anyway my project was broken by that, and hard to understand the purpose.

How do you use the widget? The default behavior of ActiveForm::begin() ActiveForm::end() is not affected by this commit.

I do like:

<?php $form = ActiveForm::begin([
...
]); ?>
...
<?php ActiveForm::end(); ?>

And in that way, all the form disappeared by your change :).

Something is wrong with your code, because content is rendered in the "Widget :: end ()" function.
I created a new project on version 2.0.16 and the authorization form is normally displayed.

Class ActiveForm you override? Is it a framework class or some other component?

My code:
use frontendformcomponentsActiveForm;
what inherits the followings:
frontendcomponentsActiveForm
yiibootstrapActiveForm
yiiwidgetsActiveForm

And I do nothing with the function run on my code.

If NDA allows, show the code frontendformcomponentsActiveForm

namespace frontend\form\components;

class ActiveForm extends \frontend\components\ActiveForm
{

    public function run()
    {
        if ($this->enableClientScript) {
            $view = $this->getView();
            \frontend\form\FormAsset::register($view);
            $js = "jQuery('#{$this->options['id']}').busForm();";
            $view->registerJs($js);
        }

        parent::run();
    }

}

return parent::run();

Okay, now works, nice! Thank you.

Just wanna say that my project was also broken by this update.
I had an extended ActiveForm and used parent::run(); in it.
And also documentation (https://www.yiiframework.com/doc/api/2.0/yii-widgets-activeform#run()-detail) says that run() method returns void.

Was this page helpful?
0 / 5 - 0 ratings