2012-05-16 24 views
1

我是YII的新用戶。我創建了一個帶有文本字段的表格,並創建了模型和粗糙生成器,我爲日期創建了文本字段。然後我用datepicker替換它,但我不知道如何將新的datepicker與模型連接起來。如何在模型類中爲datepicker編寫規則?

public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
     array('name, gender, age, dob, doj, class, 
        no_seats, train_name, proof_1, proof_2, proof_3', 'required'), 
     array('age, class, no_seats', 'numerical', 'integerOnly'=>true), 
     array('name', 'length', 'max'=>20), 
     array('gender', 'length', 'max'=>6), 
     array('train_name', 'length', 'max'=>23), 
        //I created the below statement instead of the previous //one 
       //created for text field but it is not working 
        array('dob','date','format'=>Yii::app()-> 
        locale->getDateFormat('medium')), 
     array('proof_1', 'length', 'max'=>7), 
     array('proof_2', 'length', 'max'=>8), 
     array('proof_3', 'length', 'max'=>9), 
     array('status', 'length', 'max'=>1), 
     // The following rule is used by search(). 
     // Please remove those attributes that should not be searched. 
     array('refid, name, gender, age, dob, doj, 
        class, no_seats, train_name, proof_1, proof_2, 
        proof_3, created, lastmodified, status', 'safe', 'on'=>'search'), 
    ); 
} 

回答

0

您可以使用類似於以下的日期選擇器小部件代替您擁有的任何文本輸入屬性。這將在表單和$ _POST中創建正確的字段,並準備好被模型函數操縱。請注意,您可以在模型的規則中添加驗證器「日期」。您應該在小部件和規則中使用相同的dateFormat。

<?php 
     $this->widget('zii.widgets.jui.CJuiDatePicker', array(
      'model'=>$model, 
      'attribute'=>'date', 
      // additional javascript options for the date picker plugin 
      'options'=>array(
       'showAnim'=>'fold', 
       'dateFormat'=>'yy-mm-dd', 
      ), 
      'htmlOptions'=>array(
       'style'=>'height:20px;' 
      ), 
     )); 
    ?> 

既然你是專門詢問規則:

public function rules() 
{return array(
    array('date_field', 'date'), 
);} 

這裏是驗證一個很好的總結: http://www.yiiframework.com/wiki/56/

+0

謝謝你,Narretz。 –

1
// View 
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'model'=>$model, 
    'attribute'=>'date', 
// additional javascript options for the date picker plugin 
    'options'=>array(
    'showAnim'=>'fold', 
    'dateFormat'=>'yy-mm-dd', 
    ), 
    'htmlOptions'=>array(
    'style'=>'height:20px;' 
    ), 
)); 

// add to Model rules 
array('date', 'type', 'type'=>'date', 'dateFormat'=>'dd-MM-yy'),