【问题标题】:Cannot validate textfield using codeigniter validation using callback无法使用使用回调的 codeigniter 验证来验证文本字段
【发布时间】:2019-10-14 12:47:12
【问题描述】:

我想使用 codeigniter 验证来验证文本字段。我创造了 validateSchedule 函数将在回调时验证,但这里验证不是 工作它仅在所需条件下工作。

public function validateSchedule()
{       
            $fromDate=$_POST['from_date'];  
            $toDate=$_POST['toDate'];
            if(empty($toDate) || empty($fromDate))
            {
                return TRUE;
            }
            else 
            {  

            $diffNoof_days = 10;
            if(strtotime($fromDate) > strtotime($toDate)){
                $this->form_validation->set_message('validateSchedule','from_date_must_be_smaller_than_to_date');
                return FALSE;
            }else if(strtotime($fromDate) == strtotime($toDate)){
                $this->form_validation->set_message('validateSchedule','from_date_to_must_not_be_same');
                return FALSE;
            }else if($diffNoof_days>10)
            {   
                $this->form_validation->set_message('validateSchedule','duration_should_not_exceed_10_days');
                return FALSE;
            }
            }

}

$this->form_validation->set_rules('from_date','From Date','trim|required');
$this->form_validation->set_rules('to_date','To Date','trim|required|callback_validateSchedule');

【问题讨论】:

    标签: codeigniter codeigniter-3 codeigniter-2


    【解决方案1】:

    您没有显示实际的回调,所以我推测您通过不删除 callback_ 前缀来错误地命名该方法。换句话说,定义

    public callback_validateSchedule($str)
    {
       ...
    }
    

    应该是

    public validateSchedule($str)
    {
       ...
    }
    

    如果我猜错了,请显示validateSchedule()的实际代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2014-08-30
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多