Aravind Raj
Aravind Raj

Reputation: 11

in YII How to write rules for datepicker in model class?

I'm new to YII. I created a table with text fields and created model and crud generators, I created text fields for dates. Then I replaced it with datepicker, but I don't know how to connect my new datepicker with model.

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'),
    );
}

Upvotes: 1

Views: 2073

Answers (2)

Morieskie
Morieskie

Reputation: 405

// 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'),

Upvotes: 1

Narretz
Narretz

Reputation: 4993

You can use a datepicker widget like the following in place of any text input attribute you have. This will create correct fields in the form and in $_POST, and is ready to be manipulated by model functions. Note that there is a validator 'date' that you can put in rules for your model. You should have the same dateFormat in the widget and in the rules.

 <?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;'
            ),
        )); 
    ?>

Since you were specifically asking about the rules:

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

Here is a good summary for validators: http://www.yiiframework.com/wiki/56/

Upvotes: 0

Related Questions