小部件yii \ web \ Response中的yii2 ajax验证错误

时间:2019-03-20 20:45:24

标签: php ajax yii2

使用ajax验证时,我从yii \ web \ Response中收到错误消息。

  

类yii \ web \ Response的对象无法转换为字符串

小工具:

    public function run()
{
    $model = new Participants();
    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($model);
    }
    if ($model->load(Yii::$app->request->post())) {
        $list = implode( ", ",$model->sections);
        $model->sections = $list;
        $model->save();
        Yii::$app->session->setFlash(Alert::TYPE_SUCCESS, [
            [
                'title' => 'Congratulations!',
                'text' => 'You are registered on the forum. Check out your email to get news about forum.',
                'confirmButtonText' => 'Done!',
            ]
        ]);
        return Yii::$app->controller->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
    }

    return $this->render('header', [
        'model' => $model,
    ]);

}

小部件视图:

<?php $form = ActiveForm::begin();?>
...
<?= $form->field($model, 'email', ['enableAjaxValidation' => true])->textInput(['placeholder' => 'Email']); ?>

如何解决此错误?附言yii版本-2.0.17-dev

2 个答案:

答案 0 :(得分:1)

\yii\base\Widget::run()必须返回一个字符串(基本上是所有小部件)

在方法run()中您应该做的是输出或呈现内容,并且您试图通过return ActiveForm::validate($model);return Yii::$app->controller->redirect(..)返回一个Response对象。

我建议您将所有应该处理表单提交的代码移到其自己的动作中

SiteController extends controller {


    public function actionRegisterParticipant {
        $model = new Participants();
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(Yii::$app->request->post())) {
            $list = implode(", ", $model->sections);
            $model->sections = $list;
            $model->save();
            Yii::$app->session->setFlash(Alert::TYPE_SUCCESS, [
                [
                    'title'             => 'Congratulations!',
                    'text'              => 'You are registered on the forum. Check out your email to get news about forum.',
                    'confirmButtonText' => 'Done!',
                ]
            ]);
            return Yii::$app->controller->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
        }
    // ...
}

,并且小部件视图中的表单应始终指向此操作:

<?php $form = ActiveForm::begin(['action' => 'site/register-participant']);?>
...
<?= $form->field($model, 'email', ['enableAjaxValidation' => true])->textInput(['placeholder' => 'Email']); ?>

答案 1 :(得分:0)

窗口小部件应返回字符串结果,但是return Yii::$app->controller->redirect()返回Response对象并配置了重定向。如果您想在小部件内重定向,则应使用类似的内容:

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->controller->asJson(ActiveForm::validate($model));
    Yii::$app->end();
}

// ...

Yii::$app->session->setFlash(/* ... */);
Yii::$app->controller->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
Yii::$app->end();

但是这闻起来像是一个糟糕的设计-小部件不应该负责控制应用程序流。最好在常规操作/控制器中处理用户输入。