Yii Framework Forum: Cactiveform Radiobuttonlist Required Giving Errors - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cactiveform Radiobuttonlist Required Giving Errors Rate Topic: ***** 1 Votes

#1 Guest_hrnair_*

  • Group: Guests

Posted 20 March 2013 - 12:15 PM

Hi, I am new to yii and am finding a little trouble with CActiveForm Radiobutton list.

In my model, my rules

public function rules()
	{
		return array(
			array('o_1', 'required'),
		)
	}


In my view,
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'form',
	'enableAjaxValidation'=>false,
	'enableClientValidation'=>true,
	'focus'=>array($model,'o_1'),
	'clientOptions'=>array('validateOnSubmit'=>true,),
	)); ?>
<TABLE>
	<TR>
		<TD class="first"><?php echo $form->labelEx($model,'o_1'); ?></TD>
		<?php echo $form->radioButtonList($model,'o_1', array('1a'=>$option->1a, '1b'=>$option->1b), array('template'=>'<TD class="first">{input} {label}</TD>', 'labelOptions'=>array('style'=>'display:inline'), 'separator'=>'', 'encode'=>true)); ?><TD></TD>
		</TR><TR><TD colspan="4"><?php echo $form->error($model,'o_1'); ?></TD>
	</TR>
</TABLE>
	<div class="row buttons">
		<?php echo CHtml::submitButton('Submit'); ?>
	</div>


When I select the radio button and press 'submit', Client validation error says, o_1 cannot be blank. I want to keep this radioButtonList a required selection. Please help. Thanks in advance.
0

#2 User is offline   heyhoo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 25-January 10

Posted 20 March 2013 - 01:59 PM

Same problem here with a radioButtonList.


in my case I am using a radio button list to select gender.
The HTML renders as follows:

<input id="Account_gender_0" value="M" type="radio" name="Account[gender]">
<input id="Account_gender_1" value="F" type="radio" name="Account[gender]">


looks like the problem is in jquery.yiiactiveform.js
It will look for inputID and inputID is set to Account_gender.
The appendix _0 and _1 are not taken into consideration.
0

#3 Guest_hrnair_*

  • Group: Guests

Posted 20 March 2013 - 08:05 PM

Thanks for replying.

Any idea how to solve this?
0

#4 User is offline   heyhoo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 25-January 10

Posted 21 March 2013 - 05:29 PM

Seems like this is a bug.

For now I found this work around:

I have created a hidden field:
$form->hiddenField($model, 'gender');


Now we have an hidden field that can be found by jquery.yiiactiveform.js script.

Last thing we need to do is update that hidden field when one of the radio buttons is selected.
You can do that by adding some javascript using $htmlOptions.

$form->radioButtonList($model, 'gender', array('M'=>'Male', 'F'=>'Female'), array('uncheckValue'=>null, 'onclick'=>"$('#Account_gender').val( $('input:radio:checked').val() ); "));


Replace 'Account' with your model name.
1

#5 Guest_hrnair_*

  • Group: Guests

Posted 22 March 2013 - 12:27 AM

Hi Heyhoo

Your problem can be solved in Yii way by adding inputContainer in model errors.

$form->error($model, 'attribute', array('inputContainer'=>'div'));


if there is a div.

I tried to give 'td', 'tr', 'table' etc in my case, but it is not working. In my case, there are more than one radio button lists and <table> was used to align each column with uniform width.

Any idea without having hidden fields, because the code will go more complicated.

Thanks
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users