4

我使用 CI 2.1.3 和 Wiredesignz 的 HMVC 检查了所有类似的问题,但没有一个能解决我的问题。

我的 form_validation.php 配置文件中有以下规则:

array(
    'field' => 'eta-renpal-1',
    'label' => 'Renpal number (1)',
    'rules' => 'required|callback_check_eta_group'
),

在我的 ETA 控制器中,我有这个功能(当前设置为在测试时始终无效):

public function check_eta_group($reference)
{
    // Internal function for use by form validation system to check if the ETA group requirements are met.
    $this->form_validation->set_message('check_eta_group', 'Other values in the group ' . $reference . ' are also required.');
    return false;
}

出于某种原因,“必需”函数有效,但回调无效。我已经尝试了所有其他类似的建议解决方案,但无法让它们工作。请帮忙?

编辑:回调似乎根本没有被调用。我什至在回调中做了 var_dump() 来查看屏幕上是否有输出 - 没有...

Edit2 : : 请参阅我自己的最后一条评论-使用该解决方法可以解决问题,但这并不是我想要的。所以-如果您有更好的解决方案,请分享:-)

4

2 回答 2

2

请参阅我在问题下的最后评论

(使用此处解释的解决方法,stackoverflow.com/questions/3029717/…,它有效。这不是我希望它与回调一起工作的方式,但只要它有效,它可能没问题。无论如何,谢谢。)

感谢弗罗斯蒂的评论。

于 2013-11-08T10:53:20.937 回答
0

确保您的函数检查在您实际运行自定义检查的同一控制器内(即它应该能够使用 self::check_eta_group 调用)

例如,我在使用 MY_Controller 内部的检查进行验证时遇到了麻烦。但是当我将它们移到扩展控制器中时,它工作得很好。

这是两个检查以及我如何调用它们(都在同一个控制器中)

// custom form validators for datepicker and timepicker
public function date_valid($date){
    $month = (int) substr($date, 0, 2);
    $day = (int) substr($date, 3, 2);
    $year = (int) substr($date, 6, 4);

    $this->form_validation->set_message('date_valid', 'The %s field is not a valid date');
    return checkdate($month, $day, $year);
}

public function time_valid($time){
    $this->form_validation->set_message('time_valid', 'The %s field is not a valid time');      
    if (preg_match("/^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$/i", $time)) {
        return TRUE;
    } else {
        return FALSE;
    }
}


public function create_custom(){

    // load models and libraries
    $this->load->helper(array('form', 'url'));      
    $this->load->library('form_validation');

    // set form validation rules
    $this->form_validation->set_rules('schedule_date', 'Schedule Date', 'required|callback_date_valid');
    $this->form_validation->set_rules('schedule_time', 'Schedule Time', 'required|callback_time_valid');

……

    if ($this->form_validation->run() == FALSE) { // failed validation
        error_log("validation_errors: ".validation_errors());
    }
于 2014-11-20T02:54:46.180 回答