CodeIgniter Forums
Pagination throws form validation error when clicking links - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Pagination throws form validation error when clicking links (/showthread.php?tid=53686)



Pagination throws form validation error when clicking links - El Forum - 08-03-2012

[eluser]zulubanslee[/eluser]
Using pagination library. Everything is working except when I click on one of the page links, it gets intercepted by the form validation library. Problem is it doesn't print anything, no errors nothing. Here is my controller:

Code:
function search_by_zip_result()
  {
    
    $this->load->library('zipcode_class');
    $vehicle_type     = $this->input->post('vehicle_type_search');
    $vehicle_subtype  = $this->input->post('vehicle_subtype_search');
    $vehicle_make     = $this->input->post('vehicle_make_search');
    $vehicle_model    = $this->input->post('vehicle_model_search');
    $from_price       = $this->input->post('from_price');
    $to_price         = $this->input->post('to_price');
    $from_year        = $this->input->post('from_year');
    $to_year          = $this->input->post('to_year');
    $radius           = $this->input->post('radius');
    $zip              = $this->input->post('search_by_zip');


    $this->form_validation->set_rules('search_by_zip', 'Zip Code', 'trim|required|min_length[5]|max_length[5]|integer|xss');
    $this->form_validation->set_rules('from_price', 'Price From', 'trim|integer|xss');
    $this->form_validation->set_rules('to_price', 'Price to', 'trim|integer|xss');
    if($this->form_validation->run() == FALSE)
    {
      echo 'error';
      echo validation_errors();
      exit();
    }

    $lat          = '';
    $lon          = '';
    $city         = '';
    $state        = '';
    $state_prefix = '';

    $lat_lon_rc = $this->_get_lat_long_zip($zip);
    if($lat_lon_rc)
    {
      foreach($lat_lon_rc->result() as $lat_lon_row )
       {
         $lat           = $lat_lon_row->lat;
         $lon           = $lat_lon_row->lon;
         $city          = $lat_lon_row->city;
         $state         = $lat_lon_row->state_name;
         $state_prefix  = $lat_lon_row->state_prefix;
       }
    }
  

   $zips_in_radius = $this->zipcode_class->zipcodeRadius($lat, $lon, $radius);
   if($zips_in_radius)
   {  
     // Now get all the vehicles subimtted within those zip codes.    
    
  
    // Get the count of records for pagination
     $vehicle_result_count = $this->_get_vehicles_in_radius_zip($zips_in_radius,
                                                          $vehicle_type,
                                                          $vehicle_make,
                                                          $vehicle_subtype,
                                                          $vehicle_model,
                                                          $from_year,
                                                          $to_year,
                                                          $from_price,
                                                          $to_price,
                                                          $limit = null ,
                                                          $offset  = null);

    
     if($vehicle_result_count)
     {      
       $config['base_url'] = base_url().'search/search_by_zip_result/';
       $config['total_rows'] = count($vehicle_result_count->result());
       $config['per_page'] = 3;
      
       $limit = $config['per_page'];
       $config['uri_segment'] = 4;
       $offset = $config['uri_segment'];
      
       $vehicle_result_2 = $this->_get_vehicles_in_radius_zip($zips_in_radius,  $vehicle_type, $vehicle_make, $vehicle_subtype, $vehicle_model,
                                                              $from_year, $to_year, $from_price,$to_price,
                                                              $limit, $offset);
    
       $this->pagination->initialize($config);
      
       $subview['page_links'] = $this->pagination->create_links();
      
       $data['menu']              = $this->mylib->get_menu();
       $data['sidebar_content']   = $this->mylib->_load_sidebars();
       $data['page_header']       = 'Click on the ad to see details';
       $subview['search_results'] = $vehicle_result_2;
       $data['search_results']    = $this->load->view('search/results', $subview, TRUE);
       $this->load->view('welcome_message', $data);
     }
     else
     {
       redirect(base_url().'search/search_by_zip_result',
               $this->session->set_flashdata('results_not_found', 'No results for those search criteria.' ));
     }

   }
   else
   {
     echo 'no zips codes ';
     exit();
   }  
}



Pagination throws form validation error when clicking links - El Forum - 08-03-2012

[eluser]john_j[/eluser]
Why don't you bypass the validation if it is a click from page links?


Pagination throws form validation error when clicking links - El Forum - 08-03-2012

[eluser]zulubanslee[/eluser]
How do you do that?


Pagination throws form validation error when clicking links - El Forum - 08-03-2012

[eluser]Aken[/eluser]
$this->form_validation->run() will return false on a normal page load, not just when POST values have been submitted. Your controller is done in a really weird order. It should be more like this:

Code:
if ($this->form_validation->run() === false)
{
    // A normal page load, or a form with errors goes here.
}
else
{
    // Valid form submitted, process it.
}

See the user guide example for more details.


Pagination throws form validation error when clicking links - El Forum - 08-04-2012

[eluser]zulubanslee[/eluser]
It returns the error both ways. I suspect it has something to do with uri segment but I've tried all sorts of things


Pagination throws form validation error when clicking links - El Forum - 08-04-2012

[eluser]zulubanslee[/eluser]
I think the problem is that the my query is too complicated to use the built in pagination. The pagination examples are for queries that grab one table with no parameters. The pagination does not carry over all of the parameters that I need for each click of the link.


Pagination throws form validation error when clicking links - El Forum - 08-04-2012

[eluser]Aken[/eluser]
The Pagination library has nothing to do with queries, complex or otherwise. You supply the data it uses to create links.


Pagination throws form validation error when clicking links - El Forum - 08-05-2012

[eluser]zulubanslee[/eluser]
So I have learned. I'm building parameterized query links.

Cheers.