Hi, sorry to drag out this old thread, but I think there’s a problem with the CRadioButtonList clientside validation. It doesn’t correctly perform the validation.
This is my model code fragment:
...
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('field_name, field_slug, field_type', 'required'),
array('field_slug', 'unique'),
array('field_option_group_id, form_id, visible, required, is_child', 'numerical', 'integerOnly'=>true),
array('validation', 'length', 'max'=>45),
array('field_name, field_slug', 'length', 'max'=>150),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('field_id, field_name, field_type, field_option_group_id, field_slug, visible, required, validation, is_child', 'safe', 'on'=>'search'),
);
}
}
...
My view code fragment:
...
$form=$this->beginWidget('CActiveForm', array(
'id'=>'form-field-form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
)
));
...
<div class="row">
<?php echo $form->labelEx($model,'field_type'); ?>
<?php echo $form->radioButtonList($model,'field_type', $model->getFormFieldTypes()); ?>
<?php echo $form->error($model,'field_type'); ?>
</div>
...
<?php $this->endWidget(); ?>
...
The generated HTML fragment:
...
<form id="form-field-form" action="/ccnsw/admin/formField/create" method="post">
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<label for="FormField_field_name" class="required">Field Name <span class="required">*</span></label>
<input size="60" maxlength="150" onkeyup="js:genslug($(this));" name="FormField[field_name]" id="FormField_field_name" type="text" />
</div>
<div class="row">
<label for="FormField_field_slug" class="required">Field Slug <span class="required">*</span></label>
<input size="60" maxlength="150" class="FormField" name="FormField[field_slug]" id="FormField_field_slug" type="text" />
</div>
<div class="row">
<label for="FormField_field_type" class="required">Field Type <span class="required">*</span></label>
<!-- // SEE ID below -->
<input id="ytFormField_field_type" type="hidden" value="" name="FormField[field_type]" />
<input id="FormField_field_type_0" value="text" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_0">Text field</label><br/>
<input id="FormField_field_type_1" value="checkbox" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_1">Checkbox</label><br/>
<input id="FormField_field_type_2" value="date" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_2">Date entry</label><br/>
<input id="FormField_field_type_3" value="timestamp" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_3">Timestamp</label><br/>
<input id="FormField_field_type_4" value="currency" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_4">Currency</label><br/>
<input id="FormField_field_type_5" value="list" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_5">Dropdown list</label><br/>
<input id="FormField_field_type_6" value="multi_check" type="radio" name="FormField[field_type]" />
<label for="FormField_field_type_6">Checkbox list</label><br/>
</div>
...
</form>
The generated validation JS:
...
{'id':'FormField_field_type', //<< SEE ID
'inputID':'FormField_field_type',
'errorID':'FormField_field_type_em_',
'model':'FormField','name':'FormField[field_type]',
'enableAjaxValidation':false,
'clientValidation':function(value, messages, attribute) {
if($.trim(value)=='') {
messages.push("Field Type cannot be blank.");
}
if($.trim(value)!='') {
if(value.length>45) {
messages.push("Field Type is too long (maximum is 45 characters).");
}
}
}},
...
When I submit the form with out selecting a radio button, the validation triggers and I see the message saying ‘Field Type cannot be blank’. However, if I select a radio button and re-submit, I still get the same error, when it should validate with out error.
I think it may be due to the ID not matching up between the generated HTML and JS (see where I’ve added ‘SEE ID’ comment in HTML and JS fragments above). I’m not cluey as to how the clientside validation code is meant to work, but shouldn’t it be using the ‘name’ property to check if a value is set for this group?
Let me know if something doesn’t make sense or need me to elaborate on any of it further.
Thanks! 