Radiobuttonlist. Pease, Specify Of "other" Button + Js

Hello again. First of all, thank you everyone who have helped me in my first topic here. It was very useful for me and I really do appreciate it.

I’m working on a hard (hard for me) application (registration) form whitch consists of 6-7 realted tables and about 200 fields alltogether. So, I have several questions but will divide them in different topics I think, because they are all have different issues.

So now, my first problem.

I have radioButtonList 1:


<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)')); ?>

        <div class="app_tip">(Please, specify your company type of ownership)</div>

        <?php echo $form->error($company,'company_type_of_ownership'); ?>

    </div>

With hidden <div> 1:


<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)); ?>

            <div class="app_tip"></div>

            <?php echo $form->error($company,'company_type_of_ownership_other'); ?>                

        </div>    

    </div>

radioListButton 2:


<div class="row">

        <?php echo $form->labelEx($company,'company_legal_form'); ?>

        <?php echo $form->radioButtonList($company,'company_legal_form',array("Ltd."=>"Ltd.","Joint stock"=>"Joint stock","Joint venture"=>"Joint venture","Other"=>"Other (please, specify)"),array('onchange'=>'return muFun2(this.value)')); ?>

        <div class="app_tip"></div>

        <?php echo $form->error($company,'company_legal_form'); ?>

    </div>

Hidden <div> 2:


<div id="check_2" style="display:none">                 

        <div class="row">  

            <?php echo $form->labelEx($company,'company_legal_form_other'); ?>             

            <?php echo $form->textField($company,'company_legal_form_other',array('size'=>50,'maxlength'=>50)); ?>

            <div class="app_tip"></div>

            <?php echo $form->error($company,'company_legal_form_other'); ?>                

        </div>    

    </div>

And some JS in the end:


<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;

            }

     }

function muFun2(obj){    

            if(obj=="Other"){

            document.getElementById('check_2').style.display="block";

                            return false;

            }else{

            document.getElementById('check_2').style.display="none"; 

            return false;

            }

     }     

</script>

Since I’m totally newby in JS,

First question: Is it ok to have these two functions, or I can make one from them? Anyways, it doesn’t bother me at all, because I have only 3-4 such lists.

Second question

If you choose “Other, please specify”, then hidden txtfield appears and I have two records: “Other” in ‘company_type_of_ownership’ and what user types in the shown field (‘company_type_of_ownership_other’). Perfect!

But if user selects “Other” and forgets to type something there, I have special beforeValidate code, since I can’d add ‘company_type_of_ownership_other’ to required, because it won’t then be validate if user selects NOT “Other”, but “Privat company” (this field would be left empty then):


public function beforeValidate() {

       if ($this->company_type_of_ownership=='Other' && !$this->company_type_of_ownership_other) {

            $this->addError('typeofwnership', 'Please, specify the type of ownership');

        }

       if ($this->company_legal_form=='Other' && !$this->company_legal_form_other) {

            $this->addError('legalform', 'Please, specify the company legal form');

        }

        return parent::beforeValidate();

    }

So, the user forgot to add info here and when he tries to submit form, he gets a validation error: "Please, specify the type of ownership"

BUT OUR TEXTFIELD IS HIDDEN NOW. The “Other” radio is active, but hidden textfield is not, ofcourse, because of ‘onchange’. To make this feild active again, user has to choose “Private company” and then click on “Other” again. BUT NOT EVERYONE can guess this! :(

What could be the problem solution. May be it is possible to type something like ‘onchange’||‘ifactive’ (I’m totally new to js, so can’t even imagine how it could be written). THANK YOU

No one could help? :(