radiobuttonlist as a required field

Hi

In a form I have this:




	<div class="row">

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

		<?php echo $form->radioButtonList($model,'gender',$model->getGenderOptions()); ?>

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

	</div>




and even If I select one of the options the validation complain: ‘Gender cannot be blank’

What am I missing ?

Thanks in advance.

Kind regards

Steen

This is working for me.

In Model


array('cardtype', 'required', 'message'=>'Please choose a payment method'),

In Form


            <?php

	     $cardtype = CardType::model()->filtered()->findAll();

             $cardlist = CHtml::listData($cardtype,

                    'CardTypeName', 'CardTypeName');

        echo $form->radioButtonList($model,'cardtype', $cardlist, array('class'=>'formCheckbox'));

        echo $form->error($model,'cardtype');

             ?>

Can you provide more info?

What does the generated page code look like?

doodle

I’m not sure what you need…

In my Mode I have:




	public function getGenderOptions() 

	{

		return array(1=>'Male', 2=>'Female',);

	}



The form render correctly but here is the complete page code




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<meta name="language" content="en" />


	<!-- blueprint CSS framework -->

	<link rel="stylesheet" type="text/css" href="/css/screen.css" media="screen, projection" />

	<link rel="stylesheet" type="text/css" href="/css/print.css" media="print" />

	<!--[if lt IE 8]>

	<link rel="stylesheet" type="text/css" href="/css/ie.css" media="screen, projection" />

	<![endif]-->


	<link rel="stylesheet" type="text/css" href="/css/main.css" />

	<link rel="stylesheet" type="text/css" href="/css/form.css" />


	<link rel="stylesheet" type="text/css" href="/assets/c5600351/jqueryslidemenu.css" />

<script type="text/javascript" src="/assets/224a6c1e/jquery.js"></script>

<script type="text/javascript" src="/assets/224a6c1e/jquery.yiiactiveform.js"></script>

<script type="text/javascript" src="/assets/c5600351/jqueryslidemenu.js"></script>

<title>My Web Application - Lav slankeplan</title>

</head>


<body>

	

<div class="container" id="page">


	<div id="header">

		<div id="logo">My Web Application</div>

	</div><!-- header -->


	<!-- <div id="mainmenu"> -->

	<div id="myslidemenu" class="jqueryslidemenu">

		<ul id="yw0">

<li><a href="/site/index">Home</a></li>

<li><a href="/site/page/view/about">About</a></li>

<li><a href="/site/contact">Contact</a></li>

<li><a href="/slankeplan/#">Recepies</a>

<ul>

<li><a href="/ingredients">Ingredients</a></li>

<li><a href="/site/visopskrift/retnr/20">visopskrift</a></li>

</ul>

</li>

<li><a href="/slankeplan/#">Slankeplan</a>

<ul>

<li><a href="/slankeplan/createplan">Lav slankeplan</a></li>

</ul>

</li>

<li><a href="/site/logout">Logout (demo)</a></li>

</ul>		<br style="clear: left" />

	</div><!-- mainmenu -->

			<div class="breadcrumbs">

<a href="/">Home</a> &raquo; <span>Lav slankeplan</span></div><!-- breadcrumbs -->

	

	<div class="container">

	<div id="content">

		


<h1>Lav slankeplan</h1>


<p>:</p>


<div class="form">

<form id="creatplan-form" action="/slankeplan/createplan" method="post">

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<fieldset>

	<div class="row">

		<label for="BuildSlankeplanForm_weight" class="required">Vægt <span class="required">*</span></label>		<input name="BuildSlankeplanForm[weight]" id="BuildSlankeplanForm_weight" type="text" />		<div class="errorMessage" id="BuildSlankeplanForm_weight_em_" style="display:none"></div>	</div>


	<div class="row">

		<label for="BuildSlankeplanForm_height" class="required">Højde <span class="required">*</span></label>		<input name="BuildSlankeplanForm[height]" id="BuildSlankeplanForm_height" type="text" />		<div class="errorMessage" id="BuildSlankeplanForm_height_em_" style="display:none"></div>	</div>

	<div class="row">

		<label for="BuildSlankeplanForm_age" class="required">Alder <span class="required">*</span></label>		<input name="BuildSlankeplanForm[age]" id="BuildSlankeplanForm_age" type="text" />		<div class="errorMessage" id="BuildSlankeplanForm_age_em_" style="display:none"></div>	</div>


	<div class="row">

		<label for="BuildSlankeplanForm_gender" class="required">Gender <span class="required">*</span></label>		<input id="ytBuildSlankeplanForm_gender" type="hidden" value="" name="BuildSlankeplanForm[gender]" /><input id="BuildSlankeplanForm_gender_0" value="1" type="radio" name="BuildSlankeplanForm[gender]" /> <label for="BuildSlankeplanForm_gender_0">Male</label><br/>

<input id="BuildSlankeplanForm_gender_1" value="2" type="radio" name="BuildSlankeplanForm[gender]" /> <label for="BuildSlankeplanForm_gender_1">Female</label>		<div class="errorMessage" id="BuildSlankeplanForm_gender_em_" style="display:none"></div>	</div>


	<div class="row">

		<label for="BuildSlankeplanForm_activityhome" class="required">Aktivitets niveau i fritiden <span class="required">*</span></label>		<input id="ytBuildSlankeplanForm_activityhome" type="hidden" value="" name="BuildSlankeplanForm[activityhome]" /><input id="BuildSlankeplanForm_activityhome_0" value="1" type="radio" name="BuildSlankeplanForm[activityhome]" /> <label for="BuildSlankeplanForm_activityhome_0">Ingen aktivitet - Anvender altid bil, ingen energiforbrugende hobby hjemme, dyrker ikke motion</label><br/>

<input id="BuildSlankeplanForm_activityhome_1" value="2" type="radio" name="BuildSlankeplanForm[activityhome]" /> <label for="BuildSlankeplanForm_activityhome_1">Uregelmæssig aktivitet - Uregelmæssig aktivitet (hverdagsaktivitet eller motion) med lav eller moderat intensitet 1-3 timer/uge</label><br/>

<input id="BuildSlankeplanForm_activityhome_2" value="3" type="radio" name="BuildSlankeplanForm[activityhome]" /> <label for="BuildSlankeplanForm_activityhome_2">Daglig aktivitet - Regelmæssig aktivitet (hverdagsaktivitet eller  motion), 30-60 minutter/dag, 4-7 timer om ugen.</label><br/>

<input id="BuildSlankeplanForm_activityhome_3" value="4" type="radio" name="BuildSlankeplanForm[activityhome]" /> <label for="BuildSlankeplanForm_activityhome_3">Konkurrenceidræt - Moderat eller intensiv daglig motion, mere end 7 timer/uge.</label>		<div class="errorMessage" id="BuildSlankeplanForm_activityhome_em_" style="display:none"></div>	</div>


	<div class="row">

		<label for="BuildSlankeplanForm_activityjob" class="required">Arbejds form <span class="required">*</span></label>		<input id="ytBuildSlankeplanForm_activityjob" type="hidden" value="" name="BuildSlankeplanForm[activityjob]" /><input id="BuildSlankeplanForm_activityjob_0" value="1" type="radio" name="BuildSlankeplanForm[activityjob]" /> <label for="BuildSlankeplanForm_activityjob_0">Let - Stillesiddende arbejde, ex. kontorarbejde</label><br/>

<input id="BuildSlankeplanForm_activityjob_1" value="2" type="radio" name="BuildSlankeplanForm[activityjob]" /> <label for="BuildSlankeplanForm_activityjob_1">Moderat - Stående arbejde, ex. butiksarbejde</label><br/>

<input id="BuildSlankeplanForm_activityjob_2" value="3" type="radio" name="BuildSlankeplanForm[activityjob]" /> <label for="BuildSlankeplanForm_activityjob_2">Hård - Gående arbejde, ex. håndtering af tunge ting</label>		<div class="errorMessage" id="BuildSlankeplanForm_activityjob_em_" style="display:none"></div>	</div>


	<div class="row buttons">

		<input type="submit" name="yt0" value="Lav plan" />	</div>

</fieldset>

</form></div><!-- form -->	</div><!-- content -->

</div>


	<div id="footer">

		Copyright &copy; 2011 by My Company.<br/>

		All Rights Reserved.<br/>

		Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>.	</div><!-- footer -->


</div><!-- page -->


<script type="text/javascript">

/*<![CDATA[*/

jQuery(function($) {

$('#creatplan-form').yiiactiveform({'validateOnSubmit':true,'attributes':[{'id':'BuildSlankeplanForm_weight','inputID':'BuildSlankeplanForm_weight','errorID':'BuildSlankeplanForm_weight_em_','model':'BuildSlankeplanForm','name':'BuildSlankeplanForm[weight]','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("V\u00e6gt cannot be blank.");

}


}},{'id':'BuildSlankeplanForm_height','inputID':'BuildSlankeplanForm_height','errorID':'BuildSlankeplanForm_height_em_','model':'BuildSlankeplanForm','name':'BuildSlankeplanForm[height]','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("H\u00f8jde cannot be blank.");

}


}},{'id':'BuildSlankeplanForm_age','inputID':'BuildSlankeplanForm_age','errorID':'BuildSlankeplanForm_age_em_','model':'BuildSlankeplanForm','name':'BuildSlankeplanForm[age]','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("Alder cannot be blank.");

}


}},{'id':'BuildSlankeplanForm_gender','inputID':'BuildSlankeplanForm_gender','errorID':'BuildSlankeplanForm_gender_em_','model':'BuildSlankeplanForm','name':'BuildSlankeplanForm[gender]','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("Gender cannot be blank.");

}


}},{'id':'BuildSlankeplanForm_activityhome','inputID':'BuildSlankeplanForm_activityhome','errorID':'BuildSlankeplanForm_activityhome_em_','model':'BuildSlankeplanForm','name':'BuildSlankeplanForm[activityhome]','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("Aktivitets niveau i fritiden cannot be blank.");

}


}},{'id':'BuildSlankeplanForm_activityjob','inputID':'BuildSlankeplanForm_activityjob','errorID':'BuildSlankeplanForm_activityjob_em_','model':'BuildSlankeplanForm','name':'BuildSlankeplanForm[activityjob]','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("Arbejds form cannot be blank.");

}


}}]});

});

/*]]>*/

</script>

</body>

</html>



So it is not possible to have a radioButtonlist in a form ??

cannot be true

Have you tried using different data other than the integers for the value.

My example is working fine, here is the output xhtml for comparison.


  Your Generated output

  <div class="row">

  <label for="BuildSlankeplanForm_gender" 

  class="required">Gender <span class="required">*</span>

  </label>         

  

  <input id="ytBuildSlankeplanForm_gender" 

  type="hidden" value="" name="BuildSlankeplanForm[gender]" />

  

  <input id="BuildSlankeplanForm_gender_0" value="1" type="radio" name="BuildSlankeplanForm[gender]" />

  <label for="BuildSlankeplanForm_gender_0">Male</label><br/>

  

  <input id="BuildSlankeplanForm_gender_1" value="2" type="radio" name="BuildSlankeplanForm[gender]" />

  <label for="BuildSlankeplanForm_gender_1">Female</label> 


  <div class="errorMessage" id="BuildSlankeplanForm_gender_em_" style="display:none"></div>       </div>

  

  My Generated output

  

  <input id="ytRegistrationForm_cardtype" type="hidden" value="" name="RegistrationForm[cardtype]" />

  <input class="formCheckbox" id="RegistrationForm_cardtype_0" value="Visa" type="radio" name="RegistrationForm[cardtype]" /> 

  <label for="RegistrationForm_cardtype_0">Visa</label><br/> 

  <input class="formCheckbox" id="RegistrationForm_cardtype_1" value="MasterCard" type="radio" name="RegistrationForm[cardtype]" /> 

  <label for="RegistrationForm_cardtype_1">MasterCard</label><br/> 

  <input class="formCheckbox" id="RegistrationForm_cardtype_2" value="American Express" type="radio" name="RegistrationForm[cardtype]" /> 

  <label for="RegistrationForm_cardtype_2">American Express</label><br/> 

  <input class="formCheckbox" id="RegistrationForm_cardtype_3" value="I will mail you a cheque" type="radio" name="RegistrationForm[cardtype]" /> 

  <label for="RegistrationForm_cardtype_3">I will mail you a cheque</label><br/> 

  <input class="formCheckbox" id="RegistrationForm_cardtype_4" value="Please invoice me (enter address in add. info)" type="radio" name="RegistrationForm[cardtype]" />

  <label for="RegistrationForm_cardtype_4">Please invoice me (enter address in add. info)</label>

It should work, what data type is the model field? And what do your rules look like in the model?

doodle

The point is not the rendering. The problem is that when the form is submitted, the validation fails ‘Gender cannot be blank’ even if one of the radio buttons is selected.

the rules in the model is like this:




	public function rules()

	{

		return array(

			array('weight,height,gender,age,activityhome,activityjob,plantype', 'required'),

			

		);

	}



How did you solve this problem?

I am having the same problem.

Even after I selected one option, the required field error message does not go away.

Please help!

Thank you in advance,

I also have this problem, does anyone have the answer?

create an active form, use radioBbuttonList + client validation

whenever I select any one of value, the validator always tell me "Gender cannot be blank"

what’s wrong with this issue?

yeah, I use integer:




	public function getGenderTypeOptions()

	{

		return array(

			0=>'Female', 

			1=>'Male', 

		);

	}



but I tried to use string instead of integer, it still not work properly:




	public function getGenderTypeOptions()

	{

		return array(

			'abc'=>'Female', 

			'def'=>'Male', 

		);

	}



is this a bug? anyone work validate on radioButtonList properly?

report as issue 3045

http://code.google.com/p/yii/issues/detail?id=3045