How To Change Form Fields Based On The Value Of Another Field In Yii

I’m new to Yii framework. I have a textfield that needs to be changed on radiobutton value.

i.e. if there are two radiobuttons single and Multi . This radiobuttonlist is another field ‘language’




    <div class="row" id="language">

            <?php echo $form->labelEx($model, 'language'); ?>

            <?php echo $form->radioButtonList($model, 'language',array('1' => 'Single',2 => 'Multi'));?>

            <?php echo $form->error($model, 'language'); ?>

        </div>  



Now, based on the value,

if I select single (value =1),




    <div class="row">

                <?php echo $form->labelEx($model, 'audioUrl'); ?>

                <?php echo $form->textArea($model, 'audioUrl', array('rows' => 6, 'cols' => 50)); ?>

                <?php echo $form->error($model, 'audioUrl'); ?>

            </div>

    

            <div class="row">

                <?php echo $form->labelEx($model, 'selectList'); ?>

                <?php echo $form->dropDownList($model, 'selectList', array('' => ''), array('class' => 'multiselect')); ?>

                <?php echo $form->error($model, 'selectList'); ?>

            </div>

    

    

            <div class="row">

                <?php echo $form->labelEx($model, 'Name'); ?>

                <?php echo $form->textArea($model, 'Name', array('rows' => 6, 'cols' => 50)); ?>

                <?php echo $form->error($model, 'Name'); ?>

            </div>



In the above code, select list is a multi select.

if I select single (value =2),




    <div class="row">

                <?php echo $form->labelEx($model, 'audioUrl'); ?>

                <?php echo $form->textfield($model, 'audioUrl'); ?>

                <?php echo $form->error($model, 'audioUrl'); ?>

            </div>

    

            <div class="row">

                <?php echo $form->labelEx($model, 'selectList'); ?>

                <?php echo $form->textfield($model, 'selectList'); ?>

                <?php echo $form->error($model, 'selectList'); ?>

            </div>

    

    

            <div class="row">

                <?php echo $form->labelEx($model, 'Name'); ?>

                <?php echo $form->textfield($model, 'Name'); ?>

                <?php echo $form->error($model, 'Name'); ?>

            </div>



How can I change the form fields dynamically based on radiobutton selection.

LOL … “I’m new to Yii framework” seriously all your posts. just make me wonder for how long you gonna be new to yii

Well is it wrong to mention if I have a problem and not familiar to handle such situations.

Just need to call ajax as per the radio button selection and render the selectList on the form.

there is nothing wrong with that. I am just saying you just gotta stop saying you new to yii. also what i see most of your question are not related to yii most them are general web related

how to do this?

how to do that?

Hi Rohit, I understood that ajax call should be done. But, how can I render it on select list my question

Hi alirz23, I understand what you tell. I will not repeat this again. As I’m using the radiobuttonlist to dynamically change the form fields, when I submit the form, the field values are post twice. Is it because I have repeated the fields with same id. Can you please tell me what should be done for this.

your going to need js

basically wrap each group in a new div and assign an id to each group.

hide both divs on initial view and have the js toggle divs bassed on selection.

if value = 1 show group 1

if value = 2 show group 2.

You will also need custom / conditional validation in your model to handle each variation.

You could also parse the value and count and see if the value is a single item or there are multiple and handle it that way instead of making them select single or multiple. it would be one less click for your users.