On Form Validation

hello everyone hope you are well,

I have a radio button to specify the marital status of an employee, i also have a text field to specify the spouse. I want the text field to be non editable if Unmarried is selected…

How can I do this using on form validation. Any YII methods of doing this?

In my view




<?php

                   $marital_status = array('Married' => 'Married', 'Unmarried' => 'Unmarried'); //declaration

                   echo $form->radioButtonList($model, 'marialStatus', $marital_status, //using the declared array

                   array('separator' => ' ', 'labelOptions' => array('style' => 'display:inline')));

?>


<?php echo $form->textField($model,'spouse',array('size'=>30,'maxlength'=>45,'style' => 'width:200px; height:20px;')); ?>

		



Just to disable the text field depending on the selected value…

Thanks a lot for your time…

Cheerz! :)

use javascript like :



&lt;?php


    Yii::app()-&gt;clientScript-&gt;registerScript('script','


  if(&#036;(&quot;#modelName_field&quot;).val() == 1) {


    &#036;(&quot;#modelName_other_field&quot;).attr(&quot;disabled&quot;,true);


  }


  else if(&#036;(&quot;#modelName_field&quot;).val() == 2) {


    &#036;(&quot;#modelName_other_field&quot;).attr(&quot;disabled&quot;,false);


  }


  &#036;(&quot;#modelName_field&quot;).change(function(){


    if(&#036;(this).val() == 1) {


      &#036;(&quot;#modelName_other_field&quot;).attr(&quot;disabled&quot;,true);


    } else if(&#036;(this).val() == 2) {


      &#036;(&quot;#modelName_other_field&quot;).attr(&quot;disabled&quot;,false);


    }


  });',CClientScript::POS_READY);


?&gt;

Use page source in order to get the exact key name "modelName_field".

Put this script at the end of your form page

@Zugluk - Thanks a lot for the reply, I changed the code as shown below but its not working still,




<?php

    Yii::app()->clientScript->registerScript('script','

    if($("#Academicstaff_marialStatus").val() == "Unmarried") 

    {

    $("#Academicstaff_spouse").attr("disabled",true);

    }

    else if($("#Academicstaff_marialStatus").val() == "Married") 

    {

    $("#Academicstaff_spouse").attr("disabled",false);

    }

    

    $("#Academicstaff_marialStatus").change(function()

    {

    if($(this).val() == "Unmarried") 

    {

      $("#Academicstaff_spouse").attr("disabled",true);

    } 

    else if($(this).val() == "Married") 

    {

      $("#Academicstaff_spouse").attr("disabled",false);

    }

  });',CClientScript::POS_READY);

?>



Name of my model:- Academicstaff (one word not camel case)

attribute names as in model

marialStatus and spouse.

I used the debugger and found that the JS gets initialized but still it didnt work…

Maybe im doing some mistake…

If you can point it out i’d be grateful

Thanks a lot for your time…! :)

Cheerz! :)

To be sure of spelling I encourage you first to check the source code. Input ids of fields you want to change behave have to match with #XXX you write in your script.

Moreover be sure that the value sent by form when you submitt is the same string as value you set in script "Unmarried" and "Married".

there is no reason that it do not be able to work if values match. Don’t forget to empty your cache every time you edit your script (ctrl+f5). To make more efficient debugging, you can add “alert($(”#modelName_field").val());" in order to debug if it goes well in any loop and see the value returned by the field.

@Zugluk- Im so sorry for troubling you, but i checked and rechecked, everything seems fine… :(

I think im missing something, alert doesnt work too…

Its okay, ill try figure it out…

Thanks a lot for the help…

Cheerz! :)

I changed the code to :-




Yii::app()->clientScript->registerScript('checkscript','


$("#Academicstaff_spouse").attr("disabled",true);


$("#Academicstaff_marialStatus").change(function()

    {

        if($(this).val() == "Unmarried") 

        {

        $("#Academicstaff_spouse").attr("disabled",true);

        } 

        else if($(this).val() == "Married") 

        {

      $("#Academicstaff_spouse").attr("disabled",false);

    }

  });',CClientScript::POS_READY);




Once i use an alert on spouse with a default value i get it , but on marialStatus i dont. Can someone help me get the id of marialStatus correctly?

Thanks in advance…

Cheerz! :)

how is your form to choose "Unmarried" or "Married" ?? I guess you have a table with this two entries and an id for each of them ?? not sure that value returned should be "Unmarried" and "Married" if you use a radio button it should be an array with an id or something like this.

@Zugluk - Thanks for the reply,

And nope, i have a DB field called marialStatus which is a varchar field and this is expected to get filled with either "Married" or "Unmarried" upon every insert. So in my form I had a radio button as:-




<div class="row">

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

<?php

 $marital_status = array('Married' => 'Married', 'Unmarried' => 'Unmarried'); //declaration

 echo $form->radioButtonList($model, 'marialStatus', $marital_status, //using the declared array

 array('separator' => ' ', 'labelOptions' => array('style' => 'display:inline')));

?>

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

<div>



The insert statements are working and the value "Married" or "Unmarried" is posted onto the DB upon selection…

Still could figure out the JS… it seems correct but it didnt work… :(

Anyways thanks a lot for the reply…

Really appreciate it! :)

Cheerz!

If you put an alert($(this).val()); in your function change, does it display something when you change the value martialStatus ?

does "$("#Academicstaff_spouse").attr("disabled",true);" work ?

@Zugluk- thanks for the reply,

And onchange martialStatus - it just gives me an alert with no value! :blink:

And yes,

"$("#Academicstaff_spouse").attr("disabled",true);" is working…

Can you understand on whats happening…

Please let me know if you do…

Thanks a lot…

Cheerz! :)