YII2 Modal Ajax

Asked

Viewed 478 times

0

good morning ! I am somewhat new in terms of programming, even more so in Yii2, and I know there are some guides on how to use Ajax in Yii2, but they are few and I still can’t understand easily, even without Yii2 I don’t understand ajax very well. TODAY I am guiding myself by this topic :

https://stackoverflow.com/questions/36106325/yii2-form-in-modal-window

and found the following difficulty:

After opening Modal with the form, filling in the fields and giving a Ubmit, I cannot render the form back in Modal.

NOTE: I would also like to ask, if it is not so uncomfortable, if someone could make or show me a guide in en en simple to use ajax in Yii2 or even outside it, from now on , I appreciate immensely !

Ah, my company’s network blocks access via Composer to get archive extensions

Modal Trigger in Index :

<?php echo Html::a('<span class="glyphicon glyphicon-comment"></span>',
                    ['atividade2','id' => $model->id_atividade], 
                    [
                        'title' => 'Atividades',
                        'data-toggle'=>'modal',
                        'data-target'=>'#modalvote',
                    ]
                   );
?>

Create form inside the modal :

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\modulos\GPS\models\GpsResponsaveis;
use app\modulos\GPS\models\User;
use app\modulos\GPS\models\GpsPrioridade;
use app\modulos\GPS\models\GpsStatus;
use app\modulos\GPS\models\GpsAtividades;


/* @var $this yii\web\View */
/* @var $model app\modulos\GPS\models\GpsSolicitacao */
/* @var $form yii\widgets\ActiveForm */
$this->title = 'Criar Atividade';
$this->params['breadcrumbs'][] = ['label' => 'GPS Solicitação', 'url' =>                                 ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<script     src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"     type="text/javascript"></script>
<link rel="stylesheet" type="text/css"     href="codebase/fonts/font_roboto/roboto.css"/>
<link rel="stylesheet" type="text/css" href="codebase/dhtmlxcalendar.css"/>
<script src="codebase/dhtmlxcalendar.js"></script>
<style>
.pushSpan {
margin-top: 25px;
}
</style>

<div class="box box-primary">
<div class="box-body">
<div class="gps-solicitacao-form">

    <?php
        $form = ActiveForm::begin();

        $sol_id = Yii::$app->request->get('sol_id');
        $sol_area = Yii::$app->request->get('sol_area');
        $model = new GpsAtividades();


     ?>
<div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;    </button>
        <h4 class="modal-title">Criar Atividade</h4>
      </div>
<div class="modal-body">
 <div class="col-sm-6"><!-- PRIMEIRA COLUNA -->

    <?= $form->field($model, 'responsavel')-    >dropDownList(ArrayHelper::map(User::find()->where(['id_area' => $sol_area])-    >all(), 'id', 'nome'), 
    array_merge( ['prompt' => 'Selecione'] ) ); ?>

<div class="form-group has-feedback">   
<?= $form->field($model, 'data_objetivo')->textInput(['maxlength' => true]) ?>
<span class="fa fa-calendar form-control-feedback pushSpan"></span>
</div>

<?= $form->field($model, 'prioridade')->dropDownList(ArrayHelper::map(GpsPrioridade::find()->all(), 'id_prioridade', 'nome_prioridade'), 
    array_merge( ['prompt' => 'Selecione'] ) ); ?>

</div>



 <div class="col-sm-6"><!-- SEGUNDA COLUNA -->

 <?= $form->field($model, 'descricao')->textArea(['maxlength' => true]) ?>
</div>
<div class="modal-footer">
<div class="form-group">

    <?= Html::submitButton('Criar Atividade', ['atividade','sol_id' =>         $sol_id,'class' => 'btn btn-success']) ?> 
</div>
</div>
<?php ActiveForm::end(); ?>

</div>
</div>
</div>
</div>

Controller , Actions related to Modal :

// Render the form in modal

public function actionAtividadeView()
    { 
       $model = new GpsAtividades();

       return $this->renderAjax('createAtividade', [
                'model' => $model,
        ]);

    }

// Treats the post , saves and should render the modal, I have not treated to return filled, just wish at the time it returned to the modal

public function actionAtividade()
    { 
       $model = new GpsAtividades();




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

            $model->id_solicitacao = Yii::$app->request->get('sol_id');

            $model->save();

            Yii::$app->getSession()->setFlash('success', "Atividade     Criada");  

               return $this->renderAjax('createAtividade', [
                  'model' => $model,
               ]);


          } else {
              Yii::$app->getSession()->setFlash('error', "Não foi possivel     executar esta ação");  

              return $this->renderAjax('createAtividade', [
                  'model' => $model,
               ]);
        }

    }

**I accept constructive criticism :) **

Modal antes do Submit

Renderização do modal após Submit

  • Hello. has the matter been resolved? If I understand correctly you are having as "answer" this last image without CSS, which is nothing more than the view activity/create itself. Your model is not being saved at the time of Submit, so the controller/create treatment is just "staying" on the create screen but highlighting the error occurred.

  • Try $model->save(false) to check the model cause has not been saved.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.