dpf7891 2014-12-07 20:30
浏览 39

在Yii中提交表单之前的模态用户信息

I have a form and after user submits the form and validation goes OK, I want to ask him for his email and nickname in modal window. If user fills and submit an email and nickname, I want to validate it and save it as new record or get id of existing one (in case email was already used in past). If validation is not successful, user should be able to correct values in the same modal. If everything is OK, I want to save the form including create user id.

I already have form saving and user create/find process done. I just do not know, how to put this together, to work in scenario I described above. Could anyone explain, how this should be done in Yii? I am using Yii 1.1.15 and Yii Booster. Thank you.

  • 写回答

1条回答 默认 最新

  • douyan8027 2014-12-08 04:14
    关注

    In Yii the _form.php view file is used both in update.php and create.php views by default.

    So, you might need to do smth. similar: insert form with modal in both update.php and create.php views. Actions and different for these, so you keep logic separate; this is the MVC basic advantage.

    public function actionCreate() {
        $model = new Users;
        if (isset($_POST['Users'])) {
            $model->attributes = $_POST['Users'];
            if ($model->save()) { // here in the save() method the valadation is included
                                  // ONLY after we validate and successfully saved we go to update action
                    $this->redirect(array('update', 'id' => $model->id));
            }
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }
    

    The main thing is that when you try to save save() method the validation happend automatically. So if validation is not successful the logic brings back to the same action (create for example) with fields populated in view since model is already having data passed into it: $model->attributes = $_POST['Users'].

    If validation is successful we redirect further. Not nessesary ajax form submit, even casual submit fits here.

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Users'])) {
            $model->attributes = $_POST['Users'];
            if ($model->save()) { // after saving EXISTING record we redirect to 'admin' action
                    $this->redirect(array('admin'));
            }
        }
        $this->render('update', array(
            'model' => $model,
        ));
    } 
    

    Forms in views(update/create) you keep as originally designed.

    Validation for uniqueness is simple in model rules():

    array('username, email', 'unique'),
    

    Email valadation for email syntax is seems like this:

    array('email', 'email'),
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数