Welcome Guest, Not a member yet? Register   Sign In
[ugly solved]How do you get Validation and Pagination to work together?
#1

[eluser]zimco[/eluser]
I have a search form that returns paged results and have been struggling with the logic for getting the form_validation class and the pagination class to correctly work together.

It finds and loads the first page of results fine, but when you click the next page link it fails validation everytime. Why?

If i make the validation check like:

Code:
if ( $this->form_validation->run() == FALSE  AND !$this->session->userdata('keywords') )

then the paging works, but validation fails as it lets me enter a blank form, or invalid characters.

I have already searched the forum and read many posts relating to this problem but did not find a clear solution to the problem. Does anybody have a simple working example they could share?

Here is my controller below. What am i doing wrong? What is the correct logic?
Code:
function index()
{
    $data['css'] = $this->css;
       $data['base'] = $this->base;
       $data['loadmyjavascript'] = FALSE;
      
   #Validations
   $this->load->library('form_validation');
   $this->form_validation->set_rules('searchbox', 'Search Box', 'required|alpha_space|encode_php_tags|xss_clean');

   #Load proper view either for validation fail or success
   //if ( $this->form_validation->run() == FALSE  AND !$this->session->userdata('keywords') )
   if ( $this->form_validation->run() == FALSE )
   { //validation FAILED
     $data['program_count'] = $this->quickstats_model->get_stats();
     echo "FAIL";
     $this->template->display('start', $data);
   }
   else
   { //validation SUCCESS
    if($this->input->post('searchbox')) {
        $this->session->set_userdata('keywords', $this->input->post('searchbox'));
     }
    $word = array();
    $keys = $this->session->userdata('keywords');
    echo $keys;
    $word = explode(" ", $keys);
    //Initialize Pagination Class
    $this->load->library('pagination');
    $config['base_url'] = base_url().'searchform/index/';
    $config['uri_segment'] = 3;
    $config['total_rows'] = $this->Search_model->count_all_byword($word);
    $config['per_page'] = '5';
    $config['num_links'] = '15';
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';
    $this->pagination->initialize($config);

    $data['output'] = $this->Search_model->multiColSearch($word, $config['per_page'], $this->uri->segment(3,0));
       $data['pagination'] = $this->pagination->create_links();
    $data['total_rows'] = $config['total_rows'];

    $this->template->display('prog_searchresults.php', $data);
    }
  }
#2

[eluser]kosaidpo[/eluser]
take a look here
http://net.tutsplus.com/videos/screencas...agination/
#3

[eluser]zimco[/eluser]
Thanks, already been there: it's a great tutorial for getting pagination to work, but it does not show any validation being done.

I ended up doing spaghetti logic code to get validation to work with pagination:

Code:
function index()
{
    $data['css'] = $this->css;
       $data['base'] = $this->base;
       $data['loadmyjavascript'] = FALSE;
      
   #Validations
   $this->load->library('form_validation');
   $this->form_validation->set_rules('searchbox', 'Search Box', 'required|alpha_space|encode_php_tags|xss_clean');

// if session keywords are not set, it is 1st time thru
if (!$this->session->userdata('keywords')) {
   #Load proper view either for validation fail or success
   if ( ($this->form_validation->run() == FALSE) )
   { //validation FAILED
     $data['program_count'] = $this->quickstats_model->get_stats();
     echo "FAIL";
     $this->session->unset_userdata('keywords');
     $this->template->display('start', $data);
   }
   else
   { //validation SUCCESS
       if ($this->input->post('searchbox')) {
        $q = $this->input->post('searchbox');
        // set session keywords for use with paging later
        $this->session->set_userdata('keywords', $q);
    }
    $word = array();
       $word = explode(" ", $q);
       //Initialize Pagination Class
    $this->load->library('pagination');
    $config['base_url'] = base_url().'searchform/index/';
    $config['uri_segment'] = 3;
    $config['total_rows'] = $this->Search_model->count_all_byword($word);
    $config['per_page'] = '5';
    $config['num_links'] = '15';
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';
    $this->pagination->initialize($config);

       $data['output'] = $this->Search_model->multiColSearch($word, $config['per_page'], $this->uri->segment(3,0));
       $data['pagination'] = $this->pagination->create_links();
    $data['total_rows'] = $config['total_rows'];

    $this->template->display('prog_searchresults.php', $data);
    }
} else {
    // if user typed in new input to search
    if ($this->input->post('searchbox'))
    {
    $k = $this->input->post('searchbox');
           if ( ($this->form_validation->run() == FALSE) )
           { //validation FAILED
             $data['program_count'] = $this->quickstats_model->get_stats();
             //make sure to unset any previous keywords
             $this->session->unset_userdata('keywords');
            echo "FAIL2";
             $this->template->display('start', $data);
         } else { //validation SUCCESS
             //make sure to unset any old input keywords
             $this->session->unset_userdata('keywords');
             //make sure to set new input keywords
             $this->session->set_userdata('keywords', $k);
                $word = array();
                   $word = explode(" ", $k);
                   //Initialize Pagination Class
                $this->load->library('pagination');
                $config['base_url'] = base_url().'searchform/index/';
                $config['uri_segment'] = 3;
                $config['total_rows'] = $this->Search_model->count_all_byword($word);
                $config['per_page'] = '5';
                $config['num_links'] = '15';
                $config['full_tag_open'] = '<div id="pagination">';
                $config['full_tag_close'] = '</div>';
                $this->pagination->initialize($config);

                   $data['output'] = $this->Search_model->multiColSearch($word, $config['per_page'], $this->uri->segment(3,0));
                   $data['pagination'] = $this->pagination->create_links();
                $data['total_rows'] = $config['total_rows'];

                $this->template->display('prog_searchresults.php', $data);
               }
       } else { // it is a pagination search make sure to use session keywords
                $word = array();
                $keys = $this->session->userdata('keywords');
                echo $keys;
                   $word = explode(" ", $keys);
                   //Initialize Pagination Class
                $this->load->library('pagination');
                $config['base_url'] = base_url().'searchform/index/';
                $config['uri_segment'] = 3;
                $config['total_rows'] = $this->Search_model->count_all_byword($word);
                $config['per_page'] = '5';
                $config['num_links'] = '15';
                $config['full_tag_open'] = '<div id="pagination">';
                $config['full_tag_close'] = '</div>';
                $this->pagination->initialize($config);

                   $data['output'] = $this->Search_model->multiColSearch($word, $config['per_page'], $this->uri->segment(3,0));
                   $data['pagination'] = $this->pagination->create_links();
                $data['total_rows'] = $config['total_rows'];

                $this->template->display('prog_searchresults.php', $data);
           }
}

  }




Theme © iAndrew 2016 - Forum software by © MyBB