For example, we have:
<div class="row">
<?php echo $form->labelEx($company,'company_type_of_ownership'); ?>
<?php echo $form->radioButtonList($company,'company_type_of_ownership',array("Private company"=>"Private company","A"=>"A","B"=>"B","Other"=>"Other(Please specify)"),array(//SOME JS HERE MAY BE?); ?>
<?php echo $form->error($company,'company_type_of_ownership'); ?>
</div>
If user selects A, then A is being saved in the 'company_type_of_ownership' cell.
But how to realize the next feature?
User selects "Other(Please specify)" and there is a special textfield where he can type "My own type of ownership", for example and THIS value is being saved in the table.
Or (I don't know, if its a better practise): There is another special cell in the table for such case? For example, if user selects "A", then this value is being saved in the cell "TYPE OF OWNERSHIP", and cell "OTHER TYPE" is empty then. But if he selects other, then "TYPE OF OWNERSHIP" has "Other" value, and what he types is being saved in "OTHER TYPE"
Any suggestions, please?
UPDATE: Thanks for reply. Could you please tell what am i doing wrong? I try to implement it in a such way now (I'm poor in js, so have found this in the web):
<div class="row">
<?php echo $form->labelEx($company,'company_type_of_ownership'); ?>
<?php echo $form->radioButtonList($company,'company_type_of_ownership',array("Private company"=>"Private company","Other"=>"Other (please, specify)"),array('onchange'=>'return muFun(this.value)')); ?>
<?php echo $form->error($company,'company_type_of_ownership'); ?>
</div>
<div id="check_1" style="display:none">
<div class="row">
<?php echo $form->labelEx($company,'company_type_of_ownership_other'); ?>
<?php echo $form->textField($company,'company_type_of_ownership_other',array('size'=>50,'maxlength'=>25)); ?>
<?php echo $form->error($company,'company_type_of_ownership_other'); ?>
</div>
</div>
<script>
function muFun(obj){
if(obj=="Other"){
document.getElementById('check_1').style.display="block";
return false;
}else{
document.getElementById('check_1').style.display="none";
return false;
}
}
</script>
When I choose OTHER, then this txtfield becomes visible. I've created a special cell in the table for it: 'company_type_of_ownership_other' But if I type something into it, it is not being saved into my db. What could be the problem and if it the same thing you suggested? Thank you
UPDATE 2: A little nasty bug Since everything works perfect, one problem occured: You specify "Other" button and a hidden textfield appears. You decide to ignore it and go on fullfilling application form. After you press SUBMIT (SEND), the error specified in our beforeValidate rules appears. PERFECT, BUT: The hidden textfield becomes hidden again. And to make it appear again user HAS TO CLICK ON ANOTHER radio button (for example, PRIVATE COMPANY HERE) and then click back to Other - only then the hidden textfield appears. Dear Alex, i need your help. Not really everyone can guess to make these manipulations.
Add hidden textfield, when user select Other show hidden field with jvascript, then in controller check this field and create new record for it.
Update: this.value != 'Other', 'Other' - is text, value will be different. Try this
this.options[this.selectedIndex].innerHTML
or better check what value will be there
console.log(this.value);
Better change 'onchange'=>'return muFun(this.value)'
to 'onchange'=>'return muFun(this)'
. Then function will be
function muFun(sb){
if(sb.options[sb.selectedIndex].innerHTML=="Other")
document.getElementById('check_1').style.display="block";
else
document.getElementById('check_1').style.display="none";
return false;
}
And in beforeValidate method of model you should manually check that one of this two fields is filled. Remove 'required' rule for them from rules() method of model.