EllisLab text mark
Advanced Search
     
Displaying errors
Posted: 03 November 2010 08:03 PM
Joined: 2010-11-03
4 posts

I have been working on my first CodeIgniter project and have been overcoming several issues as I have progressed however I have hit another which I have been unable to find a decent solution to.

I have some code, as shown below:

$this->form_validation->set_error_delimiters('<li>''</li>');
        
$this->form_validation->set_rules('campaign_name''campaign name''trim|required|min_length[5]|xss_clean');
        
$this->form_validation->set_rules('budget_amount''daily budget''trim|numeric|required');

        if (
$this->form_validation->run() == true{
            $slug 
url_title($this->input->post('campaign_name'), 'underscore'TRUE);
            
$check $this->db->query('SELECT COUNT(*) as total FROM campaigns WHERE userid = "'.$sessionid.'" AND slug = "'.$slug.'"');
            
$row $check->row();
            if(
$row->total == 0{
                $data 
= array('userid' => $sessionid'active' => '1''slug' => $slug'name' => $this->input->post('campaign_name'),'budget' => $this->input->post('budget_amount'));
                
$this->db->insert('campaigns'$data);
                
redirect('campaign/'.$slug);
            
else {
                
//NEED TO PASS AN ERROR THROUGH HERE
            
}
        } 

The above code works fine and you will see my comment about where I would like to create a error message to pass back to the view if the condition is not met. I could easily pass this back to the view and display it on the screen however I already have

<?=validation_errors()?> 

in my view therefore was trying to avoid adding another such function which essentially serves the same purpose.

Therefore my question is, is there anyway to add to the validation_errors() function and pass through my error message even though its not strictly a validation error from the form or will I have to create another function to deal with errors outside of the form validation?

Cheers

 
Posted: 04 November 2010 06:25 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2010-03-19
330 posts

I think you have to create another function, because validation_errors() will only show when $this->form_validation->run() is FALSE, but in your code it is TRUE, so it will not show anything.

If you could check for errors before checking

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

then you could make your own validation rule (read about callback functions http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks).