Php Yii2创建两个注册表单?
我必须为不同的用户创建两个注册表单,但我对两个注册表单都使用了一个表。表是“用户”,但在这两种形式中,某些字段不同,表是相同的。但当我验证第一种形式时,第二种形式也会验证。请帮助我,我是yii2的新手Php Yii2创建两个注册表单?,php,yii2,Php,Yii2,我必须为不同的用户创建两个注册表单,但我对两个注册表单都使用了一个表。表是“用户”,但在这两种形式中,某些字段不同,表是相同的。但当我验证第一种形式时,第二种形式也会验证。请帮助我,我是yii2的新手 <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?> <?= $form->field($model, 'username') ?>
<?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,
]);
}
注册用户
注册供应商
公共职能规则()
{
返回[
['username','filter','filter'=>trim'],
['username','required'],
['fname','required'],
['mname','required'],
['lname','required'],
['username'、'unique'、'targetClass'=>'\common\models\User'、'message'=>'已使用此用户名。],
['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'=>'此电子邮件地址已被占用。],
['password','required'],
['password','string','min'=>6],
[‘指定’、‘必需’],
['designation','string','max'=>100],
[“联系人编号”,“必填”],
[‘联系人编号’、‘号码’、‘最多’=>12],
];
}
公共职能行动注册()
{
$model=新注册表单();
如果($model->load(Yii::$app->request->post()){
如果($user=$model->signup()){
返回$this->redirect(['login']);
if(Yii::$app->getUser()->login($user)){
}
}
}
$model2=新注册表单2();
如果($model2->load(Yii::$app->request->post()){
如果($user=$model2->signup()){
返回$this->redirect(['login']);
if(Yii::$app->getUser()->login($user)){
}
}
}
返回$this->render('signup'[
'model'=>$model,
'model2'=>$model2,
]);
}
现在工作完成了,我在前端模型中创建了2个signupForm.php,在siteController中创建了两个actionSignup
验证在
活动::开始中更改id
...
<?php $form = ActiveForm::begin(['id' => 'form-signup-one']); ?>
...
<?php $form = ActiveForm::begin(['id' => 'form-signup-two']); ?>
...
。。。
...
...
两个表单的id必须不同才能进行验证。我根据您的代码为表单提供id,但问题是,…有人认为我必须创建两个注册表单?单击第一个注册按钮后,它是validate firstname、middlename、lastnameremoveid
,然后尝试,不要将任何参数传递给begin()
如以下两种形式的
。不提供id将自动生成id,如w0
、w1
、w2
等。通过右键单击和检查元素进行检查。现在,我正在做一些更改,我将上载它…感谢请投票回答,这对其他人真的很有帮助。