尝试这样的事情:
在模型中:
/*...*/
$validate = array(
'your_date' => array(
'date' => array(
//Add 'ymd' to the rule.
'rule' => array('date', 'ymd'),
'message' => 'Please select a valid birth date.',
),
),
);
/*...*/
在控制器中:
//Put into a function so you can reuse the same code.
function convertDates( &$data ){
if (!empty($data['date_of_birth']) && strtotime($data['date_of_birth']) ){
$data['date_of_birth'] = date('Y-m-d', strtotime($data['date_of_birth']));
}
}
调用上述函数的函数:
public function add() {
/*...*/
//Convert the dates to YYYY-MM-DD format before attempting to save.
$this->convertDates( $this->request->data['ModelName'] );
/*...*/
}
在视图中:
/*...*/
//Make input form a text field, and give it a class of datepicker (or whatever).
echo $this->Form->input('date_of_birth', array(
'class'=>'datepicker', 'type'=>'text','label'=>'Date of Birth'
)
);
/*...*/
然后在底部添加你的初始化脚本:
<script>
//Initialize your datepicker at the bottom.
$(document).ready(function() {
$( "input.datepicker" ).datepicker({
yearRange: "-100:+50",
changeMonth: true,
changeYear: true,
constrainInput: false,
showOn: 'both',
buttonImage: "/img/calendar.png",
buttonImageOnly: true
});
});
</script>