2013-01-14 36 views
3

我已經使用窗體元素(如textFieldRow,TbTypeahead,CJuiDatePicker和TbButton)創建了一個使用窗口小部件-bootstrap.widgets.TbActiveForm的窗體。我已經實現了Ajax表單提交,它工作正常。我已經包含了一個簡單的客戶端驗證,規則在EmployeeRegister模型中定義。它只是一個簡單的驗證空字段。問題是,當我填寫必填字段爲空的表單時,表單會提交。也就是說,不執行驗證。但它顯示錯誤消息「名稱不能爲空」。或「地址不能爲空」。任何人都可以幫助我解決這個問題? 我在下面附上我的代碼。 //型號 - EmployeeRegister如何在Yii bootstrap中將客戶端驗證放在TbActiveForm中?

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"; 

回答

相關問題