如何在Yii bootstrap的TbActiveForm中进行客户端验证?

emeijp43  于 2022-11-09  发布在  Bootstrap
关注(0)|答案(1)|浏览(163)

我已经使用widget -bootstrap.widgets.TbActiveForm创建了一个表单,其中包含textFieldRow、TbTypeahead、CJuiDatePicker和TbButton等表单元素。我已经实现 AJAX 表单提交,它运行良好。我还包含了一个简单的客户端验证,规则在EmployeeRegister模型中定义。它只对空字段进行简单的验证。问题是,当我提交必填字段为空的表单时,表单被提交。也就是说,没有执行验证。但是它显示错误消息“名称不能为空。”或“地址不能为空。"。有人能帮助我解决这个问题吗??我在下面附上我的代码。// MOdel- EmployeeRegister

class EmployeeRegister extends CActiveRecord{

    public $name;
    public $address;
    public $position;
    public $joinDate;
    public $age;
    public $phone;
    public $search;
    public $id;

    private $_identity;

    public function rules(){

        return array(
            array('name,address,position,phone','required'),
            array('joinDate,age,search,id','safe'),

        );
    }

 public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

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

//查看-编辑. php

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'employeeregister-form',
//'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true),
'htmlOptions'=>array('class'=>'well',),
)); ?>

<?php echo $form->textFieldRow($model, 'name',array('class'=>'span3','style'=>'height:30px','id'=>'emp_name')); ?>

<?php echo $form->textFieldRow($model, 'address', array('class'=>'span3','style'=>'height:30px','id'=>'emp_address')); ?>

<?php // echo $form->textFieldRow($model, 'position', array('class'=>'span3','value'=>$result['position'],'style'=>'height:30px')); ?>

<?php echo "<br/> Position <br/>";?>
<?php     $this->widget('bootstrap.widgets.TbTypeahead', array(
    'model'=>$model,
    'htmlOptions'=>array('style'=>'height:30px','id'=>'emp_position'),
     'attribute'=>'position',
     'options'=>array(
     'source'=>array(
     'Junior Software Engineer','Software Engineer','Designer'),
     'items'=>4,
     'matcher'=>"js:function(item) {
     return ~item.toLowerCase().indexOf(this.query.toLowerCase());
      }",
    )));
    ?>
<?php //echo $form->textFieldRow($model, 'joinDate', array('class'=>'span3','value'=>$result['joinDate'],'id'=>'datepicker')); ?>

<?php echo "<br/> Join Date <br/>";?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
            'model'=>$model,
            'attribute'=>'joinDate',
            //'value'=>$result['joinDate']  ? $result['joinDate'] : "",
        // additional javascript options for the date picker plugin
        'options'=>array(
            'showAnim'=>'fold',
                    'dateFormat'=>'yy-mm-dd',
        ),
        'htmlOptions'=>array(
            'style'=>'height:30px;','id'=>'emp_date'
        )
    )); ?>

    <?php echo $form->textFieldRow($model, 'age', array('class'=>'span3','style'=>'height:30px','id'=>'emp_age')); ?>
    <?php echo $form->textFieldRow($model, 'phone', array('class'=>'span3','style'=>'height:30px','id'=>'emp_phone')); ?>
    <?php echo $form->textFieldRow($model, 'id', array('class'=>'span3','style'=>'height:30px; display:none;','id'=>'emp_id')); ?>

    <?php echo "<br/>";?>
    <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'ajaxSubmit', 'label'=>'Update','type'=>'primary','htmlOptions'=>array('name'=>'update_button',),
'url'=>CController::createUrl('site/update'),
'ajaxOptions'=>array(
'type'=>'POST',
'dataType'=>'json',
'data'=>'js:$("#employeeregister-form").serialize()',
'success'=>'js:function(data){
//$("#update_err").html(data);
alert(data);
  }',
'async' => true,
)); ?>

    <?php //echo $form->error($model,'name,address,position,phone'); ?>
    <?php $this->endWidget(); ?>
    <div id="update_err"></div>
    <?php echo CHtml::link('Back to List',array('site/index')); ?>

//控制器

$model= new EmployeeRegister();
$model->attributes  =   $_POST['EmployeeRegister'];
                                if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model)); 
Yii::app()->end(); 
}
$update_id= $model->id;
$name           =   $model->name;
$address    =   $model->address;
$position   =   $model->position;
$joinDate   =   $model->joinDate;
$age            =   $model->age;
$phone      =   $model->phone;

if(EmployeeRegister::model()->updateByPk($update_id, array('id'=>$update_id,'name'=>$name,'address'=>$address,'position'=>$position,'joinDate'=>$joinDate,'age'=>$age,'phone'=>$phone)))
      echo $name."'s details updated successfully";
    else 
    echo "Update failed";

相关问题