Yii在使用CHtmlRadio的时候,如果参数不正确,会隐藏一个form,导致如果radio的值有3个时,永远只能提交第一个和最后一个
太纠结了。仔细看看,确实 是有一个隐藏FORM,导致本来应该是3个radio的button,变成了6个。
找了一下资料,看了一下源码,果然。。。。有一个hidden。
/**
* Generates a radio button for a model attribute.
* If the attribute has input error, the input field's CSS class will
* be appended with {@link errorCss}.
* @param CModel $model the data model
* @param string $attribute the attribute
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
* A special option named 'uncheckValue' is available that can be used to specify
* the value returned when the radio button is not checked. By default, this value is '0'.
* Internally, a hidden field is rendered so that when the radio button is not checked,
* we can still obtain the posted uncheck value.
* If 'uncheckValue' is set as NULL, the hidden field will not be rendered.
* @return string the generated radio button
* @see clientChange
* @see activeInputField
*/
public static function activeRadioButton($model,$attribute,$htmlOptions=array())
{
self::resolveNameID($model,$attribute,$htmlOptions);
if(!isset($htmlOptions['value']))
$htmlOptions['value']=1;
if(!isset($htmlOptions['checked']) && self::resolveValue($model,$attribute)==$htmlOptions['value'])
$htmlOptions['checked']='checked';
self::clientChange('click',$htmlOptions);
if(array_key_exists('uncheckValue',$htmlOptions))
{
$uncheck=$htmlOptions['uncheckValue'];
unset($htmlOptions['uncheckValue']);
}
else
$uncheck='0';
$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
$hidden=$uncheck!==null ? self::hiddenField($htmlOptions['name'],$uncheck,$hiddenOptions) : '';
// add a hidden field so that if the radio button is not selected, it still submits a value
return $hidden . self::activeInputField('radio',$model,$attribute,$htmlOptions);
}
太纠结了。居然用unCheckValue设置一下才OK:
echo CHtml::radioButton('btn', false, array(
'value'=>'1',
'name'=>'btnname',
'uncheckValue'=>null
));
CHtml::radioButton('btn', false, array(
'value'=>'2',
'name'=>'btnname',
'uncheckValue'=>null
));
//如果是activeForm,就得这么用
echo $form->radioButton($model, 'name', array(
'value'=>1,
'uncheckValue'=>null
));
echo $form->radioButton($model, 'name', array(
'value'=>2,
'uncheckValue'=>null
));
果然纠结。。。NND

评论 (0)
暂无评论