I'm having radio button with two value i.e, Individual and Firm.
I am looking for one scenario where if radio button having value Firm is selected, then CompanyName
textinput should act as mandatory (required) field. And, when radio button having value Individual is selected, Then CompanyName
textinput should act as Optional field.
I was not getting how to do it. I tried to add addAttribute in CompanyName textinput as mandatory. but it didn't worked as RegisterForm.php
(model) is having few rules specified.
So, any idea how to do it. I'm not getting. Any help ?
register.php (view)
<?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 (model)
<?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'],
];
}