enableClientValidation在yii 1.1.14中不起作用

时间:2014-10-30 07:09:34

标签: php jquery yii

我试图在表单上使用yii的客户端验证。服务器端验证工作正常,但我无法使用客户端验证。输入默认focus也不起作用。我还在enableClientValidation => true小部件中添加了CActiveForm。我做错了什么?

此处查看文件:

<?php
/* @var $this CdAddBusinessController */
/* @var $model CdAddBusiness */
/* @var $form CActiveForm */
?>

<!--=== Content Part ===-->
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <div class="reg-header">
                  <h2>Register a new account</h2>
                  <p>Already Signed Up? Click <a href="<?php echo Yii::app()->request->baseUrl; ?>/site/clogin" class="color-green">Sign In</a> to login your account.</p>                    
            </div>

            <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'consumer-form',
            'enableClientValidation'=>true,
            'focus'=>array($model,'consumer_first_name'),
         )); ?>

        <!-- Display Sucess Messages from the Controller -->
        <?php if(Yii::app()->user->hasFlash('register')): ?> 
            <div class="flash-success" style="color:red;text-align:center;font-weight:bold;">
                <?php echo Yii::app()->user->getFlash('register'); ?>
            </div>
        <?php endif; ?>

         <div class="row">
             <div class="col-sm-6">     
            <?php echo $form->textField($model,'consumer_first_name', array('class'=>'form-control margin-bottom-20', 'placeholder'=>'First Name')); ?>
            </div>
            <div style="color:red;text-align:center;font-family:verdana;font-size:12px;font-weight:bold;">
                  <?php echo $form->error($model,'consumer_first_name'); ?>       
              </div>
             <div class="col-sm-6">     
                <?php echo $form->textField($model,'consumer_last_name', array('class'=>'form-control margin-bottom-20', 'placeholder'=>'Last Name')); ?>
            </div>
            <div style="color:red;text-align:center;font-family:verdana;font-size:12px;font-weight:bold;">
                <?php echo $form->error($model,'consumer_last_name'); ?>
            </div>
        </div>
            <div class="row">
                <div class="col-sm-6">      
                <?php echo $form->textField($model,'consumer_tin', array('class'=>'form-control margin-bottom-20', 'placeholder'=>'TIN')); ?>
             </div>

               <div style="color:red;text-align:center;font-family:verdana;font-size:12px;font-weight:bold;">
                 <?php echo $form->error($model,'consumer_tin'); ?>       
                  </div>

                  <div class="col-sm-6">        
                <?php echo $form->textField($model,'consumer_email', array('class'=>'form-control margin-bottom-20', 'placeholder'=>'Email')); ?>
               </div>

               <div style="color:red;text-align:center;font-family:verdana;font-size:12px;font-weight:bold;">
                 <?php echo $form->error($model,'consumer_email'); ?>      
                   </div>
           </div>
              <hr>

              <div class="row">
                  <div class="col-lg-6 text-right">
                  <?php echo CHtml::submitButton($model->isNewRecord ? 'Register' : 'Save', array('class'=>'btn-u')); ?>
                  </div>
              </div>
          <!--</form>-->
        </div>
    </div>
</div><!--/container-->     
<?php $this->endWidget(); ?>
<!--=== End Content Part ===-->

这是模特:

<?php


class Rconsumer extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    //public $message;

    public function tableName()
    {
        return 'cd_consumer_users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('consumer_first_name, consumer_last_name, consumer_tin, consumer_email, consumer_reg_date, status', 'required'),
            array('consumer_email', 'email'),
            array('consumer_email', 'unique'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'consumer_id' => 'Consumer',
            'consumer_first_name' => 'First Name',
            'consumer_last_name' => 'Last Name',
            'consumer_tin' => 'TIN',
            'consumer_email' => 'Email',
            'consumer_password' => 'Password',
            'consumer_repassword' => 'Retype Password',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('consumer_id',$this->consumer_id);
        $criteria->compare('consumer_first_name',$this->consumer_first_name,true);
        $criteria->compare('consumer_last_name',$this->consumer_last_name,true);
        $criteria->compare('consumer_tin',$this->consumer_tin,true);
        $criteria->compare('consumer_email',$this->consumer_email,true);
        $criteria->compare('consumer_password',$this->primary_contact_name,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return CdAddBusiness the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

1 个答案:

答案 0 :(得分:0)

您还需要在clientOptions中将validateOnSubmit设置为true。

请参阅Enable client validation Yii