I'm new to the Yii
framework, and the situation confused me. I have a main form in a view that it has some validations. In addition, I have a gridview
inside a modal in this view that it is hidden until the end user click a button in order to choose a field. The problem appeared just when the boss told me to have some search field in the modal (I decided to use filter in gridview
for this purpose). After traces I've found that the reason was form validation of the first form when the filter method of gridview is trying to send GET
request to the server to receive grid rows that related just with the filter parameters, But it's not working maybe the failure is about my understanding of MVC
pattern.
Anyway this is some part of my codes:
View:
/* this section placed inside a modal div beside the main form*/
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'filterUrl' => Yii::$app->urlManager->createUrl('catm/grid'),
'rowOptions' => function ($model, $key, $index, $grid){
return ['onclick' => 'setText(\''. $model['name'] . '\');$(\'#myModal\').modal(\'hide\')',
'style' => 'cursor: pointer',
];
}
]);
Controller:
public function actionCreate()
{
$modelCatm = new Catm();
$modelApplication = new \app\models\Application();
$modelBranch = new \app\models\Branch();
$modelConfig = new \app\models\Config();
$modelMedia = new \app\models\Media();
$modelMessage = new \app\models\Message();
$modelServiceConfig = new \app\models\ServiceConfig();
$searchMdl = new BranchSearch();
$dataPrv = $searchMdl->search(Yii::$app->request->queryParams);
$webroot = Yii::$app->basePath;
if ($modelCatm->load(Yii::$app->request->post())){
//}
//print_r($_GET);
$transaction=Yii::$app->db->beginTransaction();
try {
//Branch:
$modelBranch->attributes = $_POST['Branch'];
//echo $modelBranch->attributes['name'];
if ($modelBranch->findBySql('select * from branch where name =\'' . $modelBranch->attributes['name'] . '\'')->count()==0) {
//echo 'not exists!';
if(!$modelBranch->save(false)) {echo 'error in model branch'; return 0;};
}
//Cashless ATM:
$modelCatm->attributes = $_POST['Catm'];
if(!$modelCatm->save(false)) {echo 'error in model catm'; return 0;};
//Application:
/* file upload handling and saving the path to database*/
$modelApplication->attributes = $_POST['Application'];
$modelApplication->catm_id = $modelCatm->id;
$modelApplication->file = UploadedFile::getInstanceByName('Application[file]');
//echo $modelApplication->file->baseName;
$modelApplication->validate();
$modelApplication->file->saveAs('uploads/application/' . $modelApplication->file->baseName . '.' . $modelApplication->file->extension);
$modelApplication->filepath = $webroot . '/uploads/application/' . $modelApplication->file->name;
if(!$modelApplication->save(false)) {echo 'error in model application'; return 0;};
//Media:
/* file upload handling and saving the path to database*/
$modelMedia->attributes = $_POST['Media'];
$modelMedia->catm_id = $modelCatm->id;
$modelMedia->file = UploadedFile::getInstanceByName('Media[file]');
//echo $modelMedia->file->baseName;
$modelMedia->validate();
$modelMedia->file->saveAs('uploads/media/' . $modelMedia->file->baseName . '.' . $modelMedia->file->extension);
$modelMedia->filepath = $webroot . '/uploads/media/' . $modelMedia->file->name;
if(!$modelMedia->save(false)) {echo 'error in model media'; return 0;};
//Messages:
$modelMessage->attributes = $_POST['Message'];
$modelMessage->catm_id = $modelCatm->id;
if(!$modelMessage->save()) {echo 'error in model message'; return 0;};
$transaction->commit();
} catch (Exception $ex) {
$transaction->rollback();
}
return $this->render('view',[
'modelCatm' => $modelCatm,
'modelBranch' => $modelBranch,
'modelApplication' => $modelApplication,
'modelConfig' => $modelConfig,
'modelMedia' => $modelMedia,
'modelMessage' => $modelMessage,
'modelServiceConfig' => $modelServiceConfig,
]);
} else {
return $this->render('create', [
'modelCatm' => $modelCatm,
'modelBranch' => $modelBranch,
'modelApplication' => $modelApplication,
'modelConfig' => $modelConfig,
'modelMedia' => $modelMedia,
'modelMessage' => $modelMessage,
'modelServiceConfig' => $modelServiceConfig,
'searchModel' => $searchMdl,
'dataProvider' => $dataPrv,
]);
}
}
I will update the question with more information of my code after your helpful comments if it is required.