phpyiiclient-side-validationyii-cactiverecord

yii CActiveForm client validation not working on some field


In my yii application i have a simple form that contains two fields like this:

<?php
                        $form = $this->beginWidget('CActiveForm', array(
                            'id' => 'message-form',
                            'enableClientValidation' => true,
                            //'enableAjaxValidation' => true,
                            'clientOptions' => array(
                                'validateOnSubmit' => true,
                                'validateOnChange' => false,
                                'validateOnType' => false,
                                'errorCssClass' => 'has-error',
                                'successCssClass' => 'has-success',

                        )));
                        ?>

                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-6">
                                    <?php echo $form->labelEx($model, 'subject'); ?>
                                    <?php echo $form->textField($model, 'subject', array('class' => 'form-control')); ?>
                                    <?php echo $form->error($model, 'subject', array('class' => 'alert alert-danger')); ?>
                                </div>
                                <div class="col-md-5"></div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-6">
                                    <?php echo $form->labelEx($model, 'message'); ?>
                                    <?php echo $form->textField($model, 'message', array('class' => 'form-control', 'id'=>'message')); ?>
                                    <?php echo $form->error($model, 'message', array('class' => 'alert alert-danger')); ?>
                                </div>
                            </div>
                        </div>
                         ......
                         //remaining of code

"subject" and "message" are two fields of "Notification" model. in Notification model, i have defined this rule:

array('subject, message', 'required')

my problem is validation for "subject" works but validation for "message" not works! after submitting the form, error message of "subject" shows up but "message" does not have any error. can anyone help me solve this problem? I use Yii 1.1.15


Solution

  • Is there a reason for HTML attribute "id" => "message" on your message field? Yii CActiveForm generate a specific HTML id : modelName_fieldName for each field.

    You don't need to set id manually (like your subject field; it don't have id). Yii needs those format of ids for validations, get/post data and more things (as client/js functions).

    Remove "id" => "message" on your message field and test again.