我有一个带有两个日期字段的表单,这些字段定义了一个时间段,以开始日期和结束日期为特征,而不是强迫用户键入日期,我使用javascript日期选择器来选择这些日期,我通过回调为每个字段定义了验证规则,如下所示:
$this->form_validation->set_rules('fechainicio', 'Fecha de Inicio', 'required|callback_startDate_valid');
$this->form_validation->set_rules('fechafin', 'Fecha de Fin', 'required|callback_endDate_valid');规则运行正常,单独运行,没有问题!但是,由于这些字段定义了一个(天)时间段,我需要开始日期早于结束日期,这将是我的第三个验证规则,但是,如果日期字段是独立的,我该如何做到这一点?如果我使用post值,它们只有在选择日期后才知道,这会导致初始错误(未知数组索引),因为当您初始加载表单时,字段是空的,你知道吗?谢谢你。
发布于 2015-06-17 02:33:16
您可以在endDate函数中获取开始日期的值。
function endDate_valid($endDate)
{
$startDate = $this->input->post('fechainicio');
if($startDate > $endDate){
$this->form_validation->set_message('endDate_valid','End Date must be greater than start date');
return FALSE;
}
return TRUE;
}发布于 2015-06-17 05:36:01
我通过在日期到达之前测试日期来解决这个问题,如下所示:
if (isset($_POST['save'])){
$post_data = array();
if( $this->input->post() ){
$post_data = $this->input->post();
if( $post_data['fechainicio'] < $post_data['fechafin'] ){
$data['fechaUno'] = $post_data['fechainicio'];
$data['fechaDos'] = $post_data['fechafin'];
}else{
echo "There´s an error in dates order.";
}
}
}虽然我在isset($_POST['save'])和if( $this->input->post() )条件中看到了一些冗余,但无论如何它都可以工作,我刚刚尝试了@jagad89技巧,并且工作得很好!,谢谢!!
https://stackoverflow.com/questions/30875278
复制相似问题