2

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?

4
  • please give your controller all code Commented Dec 21, 2015 at 4:05
  • Try commenting out the 'Yii::$app->response->format = Response::FORMAT_JSON;' Commented Dec 21, 2015 at 4:06
  • @vishu: I have updated question with the Widget code as requested. Commented Dec 21, 2015 at 4:21
  • @Coz: I tried, same error persists. Commented Dec 21, 2015 at 4:22

3 Answers 3

3

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Joe, thank you so much. Now it's returning error message. But for some reason, the error message isn't getting displayed. Any thoughts on that?
@MohitBhansali It may be that you haven't told Yii where to display errrors on your form. Try adding echo $form->errorSummary(); at the top of the form. The error should display there.
3

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

2 Comments

Hi, it's returning error message now. But for some reason, the error message doesn't render below each fields. Any idea regarding same?
Check Your model rules
1

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.