dtry54612 2016-02-14 18:14
浏览 53
已采纳

Yii2创建两个注册表单?

I have to create 2 signup form for different user but,I have use one table for both signup form. table is 'User' but in both form some field are different and table is same for both.But when I validate first form that time second form also validate.please help me I am new in yii2.

<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

                    <?= $form->field($model, 'username') ?>

                    <?= $form->field($model, 'email') ?>

                    <?= $form->field($model, 'password')->passwordInput() ?>

                    <div class="form-group">
                    <button type = "submit" class = "btn btn-primary pull-right">
                    <strong>Signup User</strong>
                    </button>

                    </div>

                <?php ActiveForm::end(); ?>
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

                    <?= $form->field($model2, 'fname') ?>
                    <?= $form->field($model2, 'mname') ?>
                    <?= $form->field($model2, 'lname') ?>
                    <?= $form->field($model2, 'username') ?>
                    <?= $form->field($model2, 'email') ?>
                    <?= $form->field($model2, 'password')->passwordInput() ?>  
                    <?= $form->field($model2, 'designation') ?> 
                    <?= $form->field($model2, 'contact_no') ?>
<button type = "submit" class = "btn btn-success pull-right">
             <strong>Signup Vendor</strong>
             </button>
              <?php ActiveForm::end(); ?>
public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['fname', 'required'],
            ['mname', 'required'],
            ['lname', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],
            ['designation', 'required'],
            ['designation', 'string', 'max' => 100],
            ['contact_no', 'required'],
            ['contact_no', 'number', 'max' => 12],
        ];
    }
public function actionSignup()
    {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                return $this->redirect(['login']);
                if (Yii::$app->getUser()->login($user)) {
                }
            }
        }
       $model2 = new SignupForm2();
        if ($model2->load(Yii::$app->request->post())) {
            if ($user = $model2->signup()) {
                return $this->redirect(['login']);
                if (Yii::$app->getUser()->login($user)) {
                }
            }
        }
        return $this->render('signup', [
            'model' => $model,
            'model2' => $model2,
        ]);
    }

Now It's Work,I am create 2 signupForm.php in front end models.and in siteController create two models in actionSignup enter image description herevalidate

  • 写回答

1条回答 默认 最新

  • duanchun1909 2016-02-14 18:33
    关注

    Change id in Active::begin

    ...
    <?php $form = ActiveForm::begin(['id' => 'form-signup-one']); ?>
    
    ... 
    
    <?php $form = ActiveForm::begin(['id' => 'form-signup-two']); ?>
    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建