简体   繁体   中英

Yii2 unique validation error: Array to string conversion

I followed the exact same process mentioned on Yii2 Documentation , but I am getting Array to string conversion error on AJAX validation.

Scenario: Enable AJAX validation in SignUp form including server side validation like email unique validation.

Changes made in view

use yii\widgets\ActiveForm;

<?php $form = ActiveForm::begin([
    'id' => 'form-signup',
    'enableAjaxValidation' => true,
]); ?>

Changes made in controller

use yii\web\Response;
use yii\widgets\ActiveForm;

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
}

** SignUp Widget **

<?php
namespace frontend\components;

use Yii;
use frontend\models\SignupForm;
use common\models\User;
use yii\base\Widget;
use yii\web\Response;
use yii\widgets\ActiveForm;

/**
 * SignUpFormWidget is a widget that provides user signup functionality.
 */
class SignUpFormWidget extends Widget
{
    /**
     * @var string the widget title. Defaults to 'Register'.
     */
    public $title='Register';
    /**
     * @var boolean whether the widget is visible. Defaults to true.
     */
    public $visible = true;

    public function run()
    {
        if($this->visible) {
            $model = new SignupForm();

            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())) {
                if ($user = $model->signup()) {
                    if (Yii::$app->getUser()->login($user)) {
                        return $this->goHome();
                    }
                }
            }

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

Error

[error][yii\\base\\ErrorException:8] exception 'yii\\base\\ErrorException' with message 'Array to string conversion' \\vendor\\yiisoft\\yii2\\base\\Widget.php:107

I have tried using json_encode($error) , but its reloading page, I cannot reload the page since register form is on header under hidden div. I have created SignUpFormWidget which extends Widget.

Please suggest, what I am missing here?

Update Your Controller code as

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {

   Yii::$app->response->format = Response::FORMAT_JSON;
   return yii\helpers\Json::encode(\yii\widgets\ActiveForm::validate($model));
}

Your error is because Widget::run() method is expecting to return a string. ActiveFrom::validate() returns an array. As @DoubleH suggested above, you need to re-write your code as

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {

   Yii::$app->response->format = Response::FORMAT_JSON;
   return yii\helpers\Json::encode(\yii\widgets\ActiveForm::validate($model));
}

You are using this Yii::$app->response->format = Response::FORMAT_JSON;

it means your are getting value in array .So try to fetch value from array then validate it , or just comment it as says in comment . You Can't validate array as string so it is giving you error .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM