Yii Framework Forum: Possible Bug In Client Validation Of Cactiveform - Yii Framework Forum

Jump to content

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

Possible Bug In Client Validation Of Cactiveform Getting "field cannot be blank" constantly Rate Topic: -----

#1 User is offline   Gex 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-July 13

Posted 25 July 2013 - 03:36 AM

I was creating a CActiveForm with the ClientValidation enabled, but stepped in an error when I wrapped a RadioButtonList inside a UL container.

Whenever I submited the form I get a "field cannot be empty" message, even if the radios were checked.

My form:
$form=$this->beginWidget('CActiveForm', array(
 'id'=>'enrollment-form',
 'enableClientValidation'=>true,

 'clientOptions'=>array(
     'inputContainer'=>'ul',
     'validateOnSubmit'=>true,
 ),
));

And one of the RadioButtonLists I use:

<div id="drinks">
  <?php echo $form->label($model,'drink_id',array('class' => 'title')); ?>                
  <?php echo $form->radioButtonList($model,'drink_id', CHtml::listData($drinks, 'id', 'name'), array('container'=>'ul', 'template' => '<li class="radio_row">{input}{label}</li>','separator' => '')); ?>                
  <?php echo $form->error($model,'drink_id'); ?>
</div>

Was I making something wrong?

I studied the code of the jquery.yiiactiveform.js and found that my problem was that in the function that gets the value of the inputs, the value was not taken correctly, because the ID used to identify the inputs was not set in a span, or in the checkboxes/radios themselves (the id was set in the UL container I defined):
var getAFValue = function (o) {
    var type,
        c = [];
    if (!o.length) {
        return undefined;
    }
    if (o[0].tagName.toLowerCase() === 'span') {
        o.find(':checked').each(function () {
            c.push(this.value);
        });
        return c.join(',');
    }
    type = o.attr('type');
    if (type === 'checkbox' || type === 'radio') {
        return o.filter(':checked').val();
    } else {
        return o.val();
    }
};

So I solved this adding || o[0].tagName.toLowerCase() === 'ul':

var getAFValue = function (o) {
    var type,
        c = [];
    if (!o.length) {
        return undefined;
    }
    if (o[0].tagName.toLowerCase() === 'span' || o[0].tagName.toLowerCase() === 'ul') {
        o.find(':checked').each(function () {
            c.push(this.value);
        });
        return c.join(',');
    }
    type = o.attr('type');
    if (type === 'checkbox' || type === 'radio') {
        return o.filter(':checked').val();
    } else {
        return o.val();
    }
};

Maybe I was just making some mistake and this solution is just a crappy workaround... but maybe this is just a bug ¿?
0

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