Reputation: 1785
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?
Upvotes: 2
Views: 2974
Reputation: 4160
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));
}
Upvotes: 3
Reputation: 3893
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));
}
Upvotes: 3
Reputation: 3079
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 .
Upvotes: 1