Yii2创建两个注册表单?

时间:2016-02-14 18:14:35

标签: php yii2

我必须为不同的用户创建2个注册表单,但是我已经使用一个表格用于两个注册表单。表格是'用户'但是在两种形式中,一些字段都是不同的,并且两个表都相同。但是当我验证第一个表单时,第二个表单也验证。请帮助我,我是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,
        ]);
    }

现在它的工作,我在前端型号中创建2 signupForm.php 。在 siteController 中,在 actionSignup中创建两个模型  enter image description here验证

1 个答案:

答案 0 :(得分:2)

更改Active::begin

中的ID
...
<?php $form = ActiveForm::begin(['id' => 'form-signup-one']); ?>

... 

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

...