Yii2 Modal doesn't show validation errors


(Antonioni Giovanni9) #1

Hello everyone,

In my application i’m using a modal with a form for create new records.

This is the code:




<?= Html::button('Write Message +',['value'=>Url::to('index.php?r=message/write-message'), 'class'=>'btn btn-info', 'id' => 'modalButton']); ?>


<?php

	Modal::begin([

		'header' => '<h4>Scrivi Messaggio</h4>',

		'id' => 'modalMess',

		'size' => 'modal-lg',

	]);

	

	echo '<div id="modalContent"></div>';

	

	Modal::end();

?>




In my controller Action i render my view with Ajax




return $this->renderAjax('write-message',[

                  'model' => $message,

                      ]);



When i open my modal i see my view rendered correctly.

If i add and remove the focus on a required field the modal doesn’t show any validation errors and then , if i try to submit the form with errors, instead to show me the errors in the modal the application redirect me to the Ajax rendered view(index.php?r=message/write-message) and in there show me errors.

what could be the cause?

thanks in advance for the help ;)


(Ajaykaushiksharma087) #2

can u post you code here.may be u are missing something.


(Antonioni Giovanni9) #3

All Right.

this is the view i want to render in Ajax:




<?php

    use yii\helpers\Html;

    use yii\widgets\ActiveForm;

    use yii\helpers\ArrayHelper;

    use app\models\User;

?>


<?php

     $form = ActiveForm::begin([

            'options' =>  [

                'class' => 'form-horizontal',

                'enctype' => 'multipart/form-data'

                

            ],

        ]);

 ?>


   

    <?= $form->errorSummary($model); ?>


    <?= $form->field($model, 'title') ?>

    

    <?= $form->field($model, 'body')->textarea() ?>

    

    <?= $form->field($model, 'active')->dropDownList(['1' => 'Active Message', '0' => 'Disabilited Message']) ?>

    

    <?= $form->field($model, 'file')->fileInput()->label('Add Image') ?>


    <?= Html::dropDownList('id_user', null,

        ArrayHelper::map(User::find()->all(), 'id', 'name'),[

            'prompt' => 'To All'

        ]) ?>

    

    

    

    <div class="form-group">

        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>

    </div>




This is the controller that launch the view and save message:




public function actionWriteMessage()

    {   

        $message   = new Message();


        if($message->load(Yii::$app->request->post())&& $message->validate())

        {

          // $this->performAjaxValidation($message); tried to use this but i've the same result

		   

          $message->file = UploadedFile::getInstance($message, 'file');


          if($message->file){

            $rnd = rand(1, 9999);

            $message->file->saveAs(Yii::getAlias('@uploadsPath').'/message/image/'.$message->file->baseName.$rnd.'.'.$message->file->extension);

            $message->image = Yii::getAlias('@uploadsPath').'/message/image/'.$message->file->baseName.$rnd.'.'.$message->file->extension;

          }

          

          $message->link('sender',User::findOne(Yii::$app->user->identity->id));

          

            if(Yii::$app->request->post('id_user')){

                 $user = User::findOne(Yii::$app->request->post('id_user')) ;

                 $message->link('users',$user); 

                 echo Yii::$app->request->post('id_user');

             }


                $this->redirect(\yii\helpers\Url::toRoute('index'));

            }else


            {

              // view rendered in Ajax

              return $this->renderAjax('write-message',[

                  'model' => $message,

                      ]);

            }

    }



This is the code of the modal in the view:




<?= Html::button('Write Message +',['value'=>Url::to('index.php?r=message/write-message'), 'class'=>'btn btn-info', 'id' => 'modalButton']); ?>


<?php

	Modal::begin([

		'header' => '<h4>Write Message</h4>',

		'id' => 'modalMess',

		'size' => 'modal-lg',

	]);

	

	echo '<div id="modalContent"></div>';

	

	Modal::end();

?>



and this is the Javascript that show the modal and load the html in the #modalContent div:




$('#modalButton').click(function(){

	$('#modalMess').modal('show')

	.find('#modalContent')

	.load($(this).attr('value'));

});




Tell me if i may post something more specific ;)


(Ajaykaushiksharma087) #4

can u post message model code also,an do one thig check in side you form what is coming inside model so print your model in your form.


(Ajaykaushiksharma087) #5



public function actionWriteMessage()

    {   

        $model   = new Message();


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

        {

                           

          $model->file = UploadedFile::getInstance($model, 'file');


          if($model->file){

            $rnd = rand(1, 9999);

            $model->file->saveAs(Yii::getAlias('@uploadsPath').'/message/image/'.$model->file->baseName.$rnd.'.'.$model->file->extension);

            $model->image = Yii::getAlias('@uploadsPath').'/message/image/'.$model->file->baseName.$rnd.'.'.$model->file->extension;

          }

          

          $model->link('sender',User::findOne(Yii::$app->user->identity->id));

          

            if(Yii::$app->request->post('id_user')){

                 $user = User::findOne(Yii::$app->request->post('id_user')) ;

                 $model->link('users',$user); 

                 echo Yii::$app->request->post('id_user');

             }


                $this->redirect(\yii\helpers\Url::toRoute('index'));

            }else


            {

              // view rendered in Ajax

              return $this->renderAjax('write-message',[

                  'model' => $model,

                      ]);

            }

    }



change every where message with model and then try like above.if it works.


(Antonioni Giovanni9) #6

i tried but it doesn’t work =(


(Steve) #7

I just ran into this problem and found that giving the form an id solves the problem.

    <?php $form = ActiveForm::begin(['id' => 'category-edit']); ?>