• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation is doing both TRUE and FALSE actions (weird bug!)

I have a weird bug. Form validation is doing both TRUE and FALSE actions.

For TRUE, the database model (scrape_url_model) runs so the database is updated, but then the views are run as though form validation is FALSE.

The FALSE view (scrape_url/index) is shown instead of the view associated with a successful validation (scrape_url/form_success).

I'm getting the validation error message "The URL is already in the database" from the callback function.

Any thoughts on what I'm doing wrong?

public function index()
            $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);

                            $session_data['eventDate']) = $this->scrape_url_model->insert_decks_and_cards();

                    if ($this->input->post('last_url') == 'yes')


                    $this->load->view('templates/header', $data);  
                    $this->load->view('scrape_url/form_success', $data);

    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;


            $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;

From the logic you've posted, it's not possible for it to execute both parts of that conditional in the same call.

Please see my post [url="http://ellislab.com/forums/viewthread/234262/#1050277"]here[/url] on a good way to structure your validation controller. It may help you out.

Note that I have no "else" on the validation condition, and that we only check to see if validation passed. If it didn't, then we carry out the default action (displaying the view). Also note that I redirect after validation completes. This prevents the user resubmitting the form if they hit the back button.

Hope this helps.


How do you pass variables from the database call to the redirect page?

You can either use flashdata (see http://ellislab.com/codeigniter/user-gui...sions.html), or you can pass it as a parameter in the URL, so long as it doesn't violate your $config['permitted_uri_chars'] policy.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.