3

我使用小部件 -bootstrap.widgets.TbActiveForm 创建了一个表单,其中包含 textFieldRow、TbTypeahead、CJuiDatePicker 和 TbButton 等表单元素。我已经实现了 ajax 表单提交,它工作正常。我已经包含了一个简单的客户端验证,规则在 EmployeeRegister 模型中定义。它仅对空字段进行简单验证。问题是,当我提交必填字段为空的表单时,表单会被提交。即,不执行验证。但它显示错误消息“名称不能为空”。或“地址不能为空。”。谁能帮我解决这个问题??我在下面附上我的代码。// 模型- 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';
    }

//查看-edit.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";
4

1 回答 1

0

使用最新版本的 yii 框架可以解决你的问题 ,你可以参考这个页面

于 2013-01-18T11:14:53.023 回答