0

我有一个奇怪的错误。

表单验证同时执行 TRUE 和 FALSE 操作。

对于 TRUE,数据库模型 (scrape_url_model) 运行,因此数据库被更新,但随后视图运行,好像表单验证为 FALSE。显示的是 FALSE 视图 (scrape_url/index),而不是与成功验证关联的视图 (scrape_url/form_success)。

The URL is already in the database从回调函数中收到验证错误消息。

public function index(){
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('', '');

    $data['pageTitle'] = 'URL Scraping Tool';

    $this->form_validation->set_rules('event_url', 'URL', 'trim|required|callback_url_check');

    if ($this->form_validation->run() == FALSE){
            $this->load->view('templates/header', $data);  
            $this->load->view('scrape_url/index', $data);
            $this->load->view('templates/footer');
    }
    else {
        $this->load->library('Db_queries');
        $this->load->library('session');
        $this->load->helper('url');

        list($session_data['alert'],
        $session_data['alert_type'],
        $session_data['countUncategorizedDecks'],
        $session_data['event_id'],
        $session_data['eventDate']) = $this->scrape_url_model->insert_decks_and_cards();

        if ($this->input->post('last_url') == 'yes'){
            $this->scrape_url_model->insert_md_percentage($session_data['eventDate']);
        }
        $this->session->set_userdata($session_data);
        $this->load->view('templates/header', $data);  
        $this->load->view('scrape_url/form_success', $data);
        $this->load->view('templates/footer_ajax');    
    }
}                      

public function url_check($event_url) {
    $url_regex = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";

    if (preg_match($url_regex, $event_url) == FALSE) {
        // check to see if input is a URL
        $this->form_validation->set_message('url_check', 'Please enter a URL including "http://".');
        return FALSE;
    }
    $this->load->database();
    $sql = 'SELECT url FROM event';
    $s = $this->db->conn_id->query($sql);
    $used_urls = $s->fetchAll(PDO::FETCH_COLUMN, 0);               

    if (in_array($event_url, $used_urls)){
        $this->form_validation->set_message('url_check', 'The URL is already in the database.');
        return FALSE;
    }
    return TRUE;
}
4

1 回答 1

0

尝试使用语句:

$this->form_validation->run() == true

然后相应地编码您的条件。它会起作用,也很容易理解。

于 2013-03-23T04:10:29.883 回答