k123456
(Kassa 923)
July 4, 2013, 1:55pm
1
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!
Zugluk
(Lacffy Duck)
July 4, 2013, 2:03pm
2
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 :
<?php
Yii::app()->clientScript->registerScript('script','
if($("#modelName_field").val() == 1) {
$("#modelName_other_field").attr("disabled",true);
}
else if($("#modelName_field").val() == 2) {
$("#modelName_other_field").attr("disabled",false);
}
$("#modelName_field").change(function(){
if($(this).val() == 1) {
$("#modelName_other_field").attr("disabled",true);
} else if($(this).val() == 2) {
$("#modelName_other_field").attr("disabled",false);
}
});',CClientScript::POS_READY);
?>
Use page source in order to get the exact key name "modelName_field".
Put this script at the end of your form page
k123456
(Kassa 923)
July 4, 2013, 3:07pm
3
Zugluk:
use javascript like :
<?php
Yii::app()->clientScript->registerScript('script','
if($("#modelName_field").val() == 1) {
$("#modelName_other_field").attr("disabled",true);
}
else if($("#modelName_field").val() == 2) {
$("#modelName_other_field").attr("disabled",false);
}
$("#modelName_field").change(function(){
if($(this).val() == 1) {
$("#modelName_other_field").attr("disabled",true);
} else if($(this).val() == 2) {
$("#modelName_other_field").attr("disabled",false);
}
});',CClientScript::POS_READY);
?>
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!
Zugluk
(Lacffy Duck)
July 4, 2013, 3:50pm
4
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.
k123456
(Kassa 923)
July 4, 2013, 4:05pm
5
Zugluk:
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!
k123456
(Kassa 923)
July 4, 2013, 4:48pm
6
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!
Zugluk
(Lacffy Duck)
July 5, 2013, 1:43pm
7
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.
k123456
(Kassa 923)
July 5, 2013, 3:12pm
8
@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!
Zugluk
(Lacffy Duck)
July 5, 2013, 3:52pm
9
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 ?
k123456
(Kassa 923)
July 5, 2013, 4:25pm
10
Zugluk:
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!
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!