尝试是这样的:
在型号:
/*...*/
$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']);
/*...*/
}
在View :
/*...*/
//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>
这工作太好了,谢谢!我错过了说明如何修改日期规则行为的文档(如果有的话)。 –