2

Just want to validate my codeigniter dropdown form before adding the selected country to database. The problem is that it inserts default value 'selectcountry' into db instead of showing the message "Please choose your country." Please help me :)

Here is My Model:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_example extends CI_Model {

  function __construct()
 {
  //Call the Model constructor
   parent::__construct();
 }

public function did_add() {

        $data = array(
        'country' => $this->input->post('country')                 
                     );     
        $query = $this->db->insert('example_table', $data);

        if ($query) {
            return true;} 
        else {
            return false;}      
    }  
}

Here is My View:

     $this->load->helper("form","file","html","url");    


     echo $message;

     echo validation_errors();

     echo form_open("example/add");

     echo form_label("Country:<br>","country");
     $data = array(
          "selectcountry"  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
     echo form_dropdown('country', $data, 'selectcountry');
     echo br(1);


     echo form_submit("Submit", "Add");

     echo form_close();

Here is My controller:

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Example extends MX_Controller {

    public function index() {       
            $data["message"] = "";            
            $this->load->view("view_example",$data);        
               }

    public function add() {                     
            $this->load->library('form_validation');
            $this->load->model('model_example');            
            $this->form_validation->set_rules('country', 'Country', 'required|callback_country_check');     
            if($this->form_validation->run()){
            $this->model_example->did_add();    
            }
            else {
            $data["message"] = "";          
            $this->load->view("view_example",$data);               
            }       
           }

     public function country_check()
    {
            if ($this->input->post('country') === 'selectcountry')  {
            $this->form_validation->set_message('country_check', 'Please choose your country.');
            return FALSE;
        }
        else {
            return TRUE;
        }
    }
}

1 Answer 1

0

In your view page change this line

$data = array(
          "selectcountry"  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
  echo form_dropdown('country', $data, 'selectcountry');

to this

$data = array(
          ""  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
  echo form_dropdown('country', $data, set_value('country'));

You wont need another call back function to check if value is empty.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your help, it works in the way you described :) However, I also want to find out what I should change in my code to make that callback function work...
instead of if ($this->input->post('country') === 'selectcountry') try adding if ($this->input->post('country') == 'selectcountry') to check the condition. I am not sure but it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.