0

renderPartial clientSide validation doesn't work. I want to render part of form with ajax. Ex.: _form.php

$form = ActiveForm::begin([
    'options' => [
        'enableAjaxValidation' => true,
    ]
]); 
$form->field($model, 'category_id')->dropDownList($category, [
'onchange'=>'
    $.get( "'.Url::toRoute('/controller/params').'", { id: $(this).val() } )
           .done(function( data ) {
                     $( "#offers-param-content" ).html( data );
           }
     );'
]);

Controller.php

public function actionParams($id)
{
    $model = new Param();
    $params = EavAttribute::find()->where(['category_id'=>$id])->all();
    $this->renderPartial('_params', ['model' => $model, 'params' => $params];
}

_params.php

foreach($params as $item){
    echo Html::activeTextInput('text', $model, $item->name);
}
Sohel Ahmed Mesaniya
  • 3,344
  • 1
  • 23
  • 29
user3185208
  • 47
  • 1
  • 1
  • 7
  • Have you tried uisng [renderAjax()](http://www.yiiframework.com/doc-2.0/yii-web-view.html#renderAjax%28%29-detail)? – Insane Skull Feb 01 '16 at 04:59

3 Answers3

4

If you want to enable client validation, then set this property to true.

$form = ActiveForm::begin([
    'options' => [
        'enableAjaxValidation' => true,
    'enableClientValidation'=>true

    ]
]); 

And use renderAjax() function in place of renderPartial() it will inject into the rendering result with JS/CSS scripts and files which are registered with the view

0

In your Controller.php you need to set layout to false and die the execution

public function actionParams($id)
{
    $this->layout = false;
    $model = new Param();
    $params = EavAttribute::find()->where(['category_id'=>$id])->all();
    $this->renderPartial('_params', ['model' => $model, 'params' => $params];
    die;
}
Sohel Ahmed Mesaniya
  • 3,344
  • 1
  • 23
  • 29
0

Your not returning any validation errors from your controller to your view. To archive that use

yii\widgets\ActiveForm::validate($yourModel);

If your not using activeform you can return errors by

$model->getErrors();//will return errors from $model->validate()

From your excerpt try this

public function actionParams($id) {
        $model = new Param();
        if ($model->load(Yii::$app->request->post())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return ActiveForm::validate($model); /* Validation error messages are returned by this static function */
            }
            $params = EavAttribute::find()->where(['category_id' => $id])->all();
            $this->renderPartial('_params', ['model' => $model, 'params' => $params]);
        }
    }
chapskev
  • 972
  • 9
  • 27