我在用户加载页面时加载了两个模型,但只有一个模型会根据用户的偏好获取信息,但我的问题是当用户提交表单时提交了两个模型并且我收到错误,因为其他模型需要在信息可以继续之前获取信息,我想知道如何加载模型中的一个而忽略另一个基于用户填写的表单
位指示
public function actionSignup($mode)
{
//if($mode === 'personalAccount'){
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
// }else{
$model_business = new SignupFormbusiness();
if ($model_business->load(Yii::$app->request->post())) {
if ($user = $model_business->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
'model_business' => $model_business,
]);
// }
}
视图
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
//if(isset($model_business))
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container" style="width: 100%;">
<div class="row" style="background: #fcfcfc;">
<div class="col-md-12 login-form-top">
</div>
<div class="col-lg-12 signup-form ">
<div class="signup-form-container">
<?php $form = ActiveForm::begin(['id' => 'login-form' ]);
$loginUrl = \Yii::$app->UrlManager->createUrl(['site/login']);
?>
<h1>Sign Up</h1>
<p style="text-align: center;">Please fill in this form to create an account.<br/>
Already a Member? <a href="<?=$loginUrl;?>">Login</a>
</p>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Personal Account
<input type="radio" checked="checked" value="personal" name="acc-type" id="personal_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Business Account
<input type="radio" value="enterprise" name="acc-type" id="enterprise_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<hr>
<?php if(isset($model) && !empty($model)): ?>
<div id="personal">
<div class="col-xs-6" style="padding-left:0; padding-right:5px;">
<?= $form->field($model, 'last_name', [
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Surname'])->label(false) ?>
</div>
<div class="col-xs-6" style="padding-right:0; padding-left:5px;">
<?= $form->field($model, 'first_name', [
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Given Name'])->label(false) ?>
</div>
<div class="col-xs-12 col-nopadding">
<?= $form->field($model, 'username',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Username'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'email',[])->textInput(['placeholder'=> 'Email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'phone1',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Phone Number'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'password',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->passwordInput(['placeholder'=> 'Password'])->label(false) ?>
</div>
</div>
<?php endif;?>
<?php if(isset($model_business) && !empty($model_business)): ?>
<div id="enterprise">
<div class="col-xs-12" style="padding-right:0; padding-left:5px;">
<?= $form->field($model_business, 'business_name', [
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Business Legal name'])->label(false) ?>
</div>
<div class="col-xs-12 col-nopadding">
<?= $form->field($model_business, 'username',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Username'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'email',[])->textInput(['placeholder'=> 'Business email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'phone1',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Business Phone Number'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'password',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->passwordInput(['placeholder'=> 'Password'])->label(false) ?>
</div>
</div>
<?php endif;?>
<!--<input class="signup-input" type="password" placeholder="Repeat Password" name="psw-repeat" >-->
<div class="col-md-12 col-nopadding">
<p class="signup-condition">By creating an account you agree to our <a href="#" > Terms & Conditions</a> and <a href=""> Privacy Policy.</a></p>
</div>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
<?php
$script = <<< JS
$(document).ready(function(){
$(function (){
$("#enterprise_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").show();
$("#personal").hide();
}
});
});
$(function (){
$("#personal_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").hide();
$("#personal").show();
}
});
});
});
JS;
$this->registerJs($script);
?>
上面的两张图片是我正在尝试实现的内容的简要说明,但是表单是从不同的模型加载的,必须先填写才能验证,我只需要用户一次填写此表单中的一个这意味着另一个将是空的,无法验证哪个会触发错误,有没有办法解决这个问题或更好的解决方法。
我填写第一个表格的预分类帐户并点击提交,没有任何反应,然后我检查商业帐户表格并得到此错误,这意味着它还要我填写表格才能继续进行提交< / p>
答案 0 :(得分:1)
您应该创建自定义FormModel
或创建2个单独的表单,只要选择了相关的选择/无线电输入,就会显示出来,您正在做的是模型是不同的并且表单是合并的这两个模型的领域都是奇怪的并且会产生问题。
如果您选择创建单个FormModel
,则应使用Conditional Validation
在验证检查规则中提供when
和whenClient
。
一个快速的建议是创建两个不同的形式,如下所示,因为它们都是不同的模型,你应该将模型的字段分配给2种不同的形式,而不是试图将所有字段合并为一个单独的形式并保持模型分离
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
//if(isset($model_business))
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container" style="width: 100%;">
<div class="row" style="background: #fcfcfc;">
<div class="col-md-12 login-form-top">
</div>
<div class="col-lg-12 signup-form ">
<div class="signup-form-container">
<?php
$loginUrl = \Yii::$app->UrlManager->createUrl(['site/login']);
?>
<h1>Sign Up</h1>
<p style="text-align: center;">Please fill in this form to create an account.<br/>
Already a Member? <a href="<?= $loginUrl; ?>">Login</a>
</p>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Personal Account
<input type="radio" checked="checked" value="personal" name="acc-type" id="personal_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Business Account
<input type="radio" value="enterprise" name="acc-type" id="enterprise_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<hr>
<?php if (isset($model) && !empty($model)): ?>
<?php $form = ActiveForm::begin(['id' => 'signup-form']); ?>
<div id="personal">
<div class="col-xs-6" style="padding-left:0; padding-right:5px;">
<?=
$form->field($model, 'last_name', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Surname'])->label(false)
?>
</div>
<div class="col-xs-6" style="padding-right:0; padding-left:5px;">
<?=
$form->field($model, 'first_name', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Given Name'])->label(false)
?>
</div>
<div class="col-xs-12 col-nopadding">
<?=
$form->field($model, 'username', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Username'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'email', [])->textInput(['placeholder' => 'Email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model, 'phone1', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Phone Number'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model, 'password', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>{error}{hint}'
])->passwordInput(['placeholder' => 'Password'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<p class="signup-condition">By creating an account you agree to our <a href="#" > Terms & Conditions</a> and <a href=""> Privacy Policy.</a></p>
</div>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
<?php endif; ?>
<?php if (isset($model_business) && !empty($model_business)): ?>
<?php $form = ActiveForm::begin(['id' => 'signup-business']); ?>
<div id="enterprise">
<div class="col-xs-12" style="padding-right:0; padding-left:5px;">
<?=
$form->field($model_business, 'business_name', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Business Legal name'])->label(false)
?>
</div>
<div class="col-xs-12 col-nopadding">
<?=
$form->field($model_business, 'username', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Username'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'email', [])->textInput(['placeholder' => 'Business email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model_business, 'phone1', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Business Phone Number'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model_business, 'password', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->passwordInput(['placeholder' => 'Password'])->label(false)
?>
</div>
</div>
<div class="col-md-12 col-nopadding">
<p class="signup-condition">By creating an account you agree to our <a href="#" > Terms & Conditions</a> and <a href=""> Privacy Policy.</a></p>
</div>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
$script = <<< JS
$(document).ready(function(){
$(function (){
$("#enterprise_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").show();
$("#personal").hide();
}
});
});
$(function (){
$("#personal_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").hide();
$("#personal").show();
}
});
});
});
JS;
$this->registerJs($script);
?>
控制器代码
function actionSignup($mode)
{
$model = new SignupForm();
$model_business = new SignupFormbusiness();
if (Yii::$aap->request->isPost) {
$accountType = Yii::$app->request->post("acc-type");
if ($accountType == 'personal' && $model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
if ($accountType == 'enterprise' && $model_business->load(Yii::$app->request->post())) {
if ($user = $model_business->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
}
return $this->render('signup', [
'model' => $model,
'model_business' => $model_business,
]);
}
答案 1 :(得分:0)
希望默认情况下为两个表单激活验证,将应用以下解决方案
首先,给出两个表单不同的id,如first_form_id和secod_form_id,如下所示:
//For the first form:
<?php $form = ActiveForm::begin(['id' => 'first_form_id']); ?>
//For the second form:
<?php $form = ActiveForm::begin(['id' => 'second_form_id']); ?>
然后您可以修改视图文件的最后一部分,如下所示:
<?php
$script = <<< JS
$(document).ready(function(){
$(function (){
$("#enterprise_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").show();
$("#personal").hide();
// Here we are disabling the validation for the first form;
$('#first_form_id').yiiActiveForm('validate', false);
//Activate the validation of the second form just incase it was disabled
$('#second_form_id').yiiActiveForm('validate', true);
}
});
});
$(function (){
$("#personal_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").hide();
$("#personal").show();
//Activate the validation of the first form
$('#first_form_id').yiiActiveForm('validate', true);
//De-activate the validation of the first form just incase it was disabled
$('#second_form_id').yiiActiveForm('validate', false);
}
});
});
});
JS;
$this->registerJs($script);
?>