我有两个值的单选按钮,即个人和公司。我正在寻找一种情况,如果选择了具有值 Firm 的单选按钮,则CompanyName
textinput 应充当强制(必需)字段。And, when radio button having value Individual is selected, Then CompanyName
textinput should act as Optional field.
我不知道该怎么做。我尝试在 CompanyName 文本输入中添加 addAttribute 作为强制性。但它没有起作用,因为RegisterForm.php
(模型)指定的规则很少。
所以,任何想法如何做到这一点。我不明白。有什么帮助吗?
register.php(查看)
<?php $form = ActiveForm::begin(['id' => 'register-form']); ?>
.
.
.
<?= $form->field($model, 'AdminType')
->radioList(array('Individual'=>'An Individual', 'Firm'=>'Firm/Company/Group of Employees'))
->label('Are You')?>
<?= $form->field($model, 'CompanyName')->textInput()->label('Company Name') ?>
<div class="form-group">
<?= Html::submitButton('Register', ['class' => 'btn btn-success', 'name' => 'register-button' ]) ?>
</div>
<?php ActiveForm::end(); ?>
<script>
$('input[type="radio"]').click(function()
{
if($(this).attr("value")=="Firm")
{
$('#registerform-companyname').addAttribute("required");
}
});
</script>
RegisterForm.php(模型)
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use kartik\password\StrengthValidator;
class RegisterForm extends Model
{
public $fname;
public $lname;
public $email;
public $password;
public $confirmPassword;
public $AdminType;
public $CompanyName;
public $verifyCode;
public function rules()
{
return [
[['fname','lname', 'email', 'password','confirmPassword','verifyCode','AdminType'], 'required'],
['email', 'email'],
['confirmPassword', 'compare', 'compareAttribute' => 'password'],
['verifyCode', 'captcha'],
];
}