Welcome Guest, Not a member yet? Register   Sign In
Form validation fails to prevent data entry to db
#1

[eluser]alex646[/eluser]
Hey guys,

Can somebody help me with the following issue I have:

Form validation fails to prevent data entry to db after lets say I omit required field. I get the form_error messages that field is required but form still inserts data to db which is blank.

Maybe I'm missing something : (

Thanks!
My controller that handles the form validation
Code:
public function newpost(){
        
        $title = $this->input->post('title');
        $post = $this->input->post('post');
        $this->load->model('private/add_post_model');
        $this->add_post_model->add_post($title, $post);
        
        //$this->form_validation->set_error_delimiters('', '');

        $this->form_validation->set_rules('title', 'Title', 'required|trim|min_length[6]');
        $this->form_validation->set_rules('post', 'Post', 'required|trim|min_length[6]');

        
        if ($this->form_validation->run() == FALSE)
        {
        $this->template->set_theme(Settings_model::$db_config['default_theme']);
        $this->template->set_layout('main');
        $this->template->title('Add post');
        $this->process_partial('header', 'header');
        $this->process_partial('footer', 'footer');    
        $this->process_template_build('private/add_post_form');
        
        }
        
            else
        {
        
        $this->template->set_theme(Settings_model::$db_config['default_theme']);
        $this->template->set_layout('main');
        $this->template->title('Add post');
        $this->process_partial('header', 'header');
        $this->process_partial('footer', 'footer');    
        $this->process_template_build('private/form_success');  }
}

My model:
Code:
class Add_post_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }


    public function add_post($title, $post) {

        if ($title!=NULL && $post!=NULL){
       // $uid = $this->_get_userid();  //get current logged in user id from db
        $this->db->set('title', $title);
        $this->db->set('post', $post);
       // $this->db->set('user_id', $uid); // e.g. username = 'administrator'
        $this->db->set('user_id',  $this->session->userdata('id')); // e.g. username = 'administrator'

        $query = $this->db->insert('posts');
        }
        
    }
#2

[eluser]CroNiX[/eluser]
Because you're getting $title and $post from input::post(), which never returns NULL. They will return the value or boolean FALSE if it doesn't exist.
Quote:CodeIgniter comes with three helper functions that let you fetch POST, COOKIE or SERVER items. The main advantage of using the provided functions rather than fetching an item directly ($_POST['something']) is that the functions will check to see if the item is set and return false (boolean) if not.
However, I'm not sure why you are running validation on those fields and ALSO within your add_post(). You don't need the checking within add_post(), just move add_post() to be within your validation success statement.

Code:
if ($this->form_validation->run() == FALSE)
{
  //your failed stuff
}
else
{
  //success!  Write to DB...
  $this->load->model('private/add_post_model');
  $this->add_post_model->add_post($title, $post);

  //your other success stuff...
}
Code:
public function add_post($title, $post) {

        //if ($title!=NULL && $post!=NULL){  //NOT NEEDED because this was done in validation
       // $uid = $this->_get_userid();  //get current logged in user id from db
        $this->db->set('title', $title);
        $this->db->set('post', $post);
       // $this->db->set('user_id', $uid); // e.g. username = 'administrator'
        $this->db->set('user_id',  $this->session->userdata('id')); // e.g. username = 'administrator'

        $query = $this->db->insert('posts');
       // }
    }
#3

[eluser]alex646[/eluser]
Thanks a lot!

I'm learning ci and some things are just harder to grasp at first than others, but your
post helped me to get my head around it : )

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB