Priyanka Ahire
Priyanka Ahire

Reputation: 502

Yii2 create two signup form?

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

Upvotes: 2

Views: 1416

Answers (1)

Sohel Ahmed Mesaniya
Sohel Ahmed Mesaniya

Reputation: 3450

Change id in Active::begin

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

... 

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

...

Upvotes: 2

Related Questions