douzhuo2002 2015-05-12 12:25
浏览 78

yii2 GridView过滤与同一页面中的其他表单冲突

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.

  • 写回答

1条回答 默认 最新

  • duan01203 2015-05-12 23:15
    关注

    It has nothing to do with your understanding of MVC mate, Yii does a lot of things for you automatically but sometimes in cases like this you just need to get to do some things by hand.

    Probably you need to change the form action in your controller to check if this is a form submission or a gridview navigation / filter and handle it differently. You need to check if some get params exist and bypass the form validation in that case.

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测