Jquery ui 使用JQuery Datepicker进行CakePHP日期验证
我正在尝试将JQuery的datepicker与CakePHP结合使用。我似乎不知道该怎么做 同时还使用CakePHP的内置日期验证,并允许任何日期格式。 我遇到的任何答案都涉及复杂的解决方案。 我觉得必须有一种简单的方法来实现这一点,这似乎是一个JQuery 日期选择器是一个很常见的东西 目前,我正在尝试将日期转换为CakePHP日期数组 格式 当输入无效数据时,这会导致问题。它在任何空日期中放置两个空格 字段,strotime将空间计数为“现在”。所以如果是无效数据 如果输入,并且用户重新发送数据,则会导致任何空的日期字段(不是必需的) 保存为今天的日期Jquery ui 使用JQuery Datepicker进行CakePHP日期验证,jquery-ui,cakephp,cakephp-2.1,Jquery Ui,Cakephp,Cakephp 2.1,我正在尝试将JQuery的datepicker与CakePHP结合使用。我似乎不知道该怎么做 同时还使用CakePHP的内置日期验证,并允许任何日期格式。 我遇到的任何答案都涉及复杂的解决方案。 我觉得必须有一种简单的方法来实现这一点,这似乎是一个JQuery 日期选择器是一个很常见的东西 目前,我正在尝试将日期转换为CakePHP日期数组 格式 当输入无效数据时,这会导致问题。它在任何空日期中放置两个空格 字段,strotime将空间计数为“现在”。所以如果是无效数据 如果输入,并且用户重新发
有什么想法吗?试试这样的方法: 在模型中:
/*...*/
$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>
//在底部初始化日期选择器。
$(文档).ready(函数(){
$(“input.datepicker”).datepicker({
年范围:“-100:+50”,
变化月:对,
变化年:是的,
输入:false,
showOn:“两者都有”,
buttonImage:“/img/calendar.png”,
buttonImageOnly:true
});
});
效果很好,谢谢!我错过了说明如何修改日期规则行为的文档(如果有)。
<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>