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 