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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化