• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation & view within a one function

#1
Alright this question may sound silly & sorry for that as i am a  newbie to CI. 

Here's my problem, right now to validate & show a form am i using 2 functions in one of my controllers.

for example I have a controller named "Customer". the web application i am working on have feature to "Add customers" mainly the adding a customer done via a form interface.

so in my controller (Customer) i have 2 functions like this.

1. public function new_vehicle() - This function loads the view with add customer form.

2.  public function validate_vehicle() - This function contains validation rules related to add customer form.

in the view forms action (echo form_open) is set to validate_vehicle function, this way when the user submits the form if there are errors the form will not send results to database if no errors the results will be send to database.

when a user needs to add a customer, the form can be accessed by MYSITE.com/Customer/new_vehicle, as i defined the view with this function.

when there are validation errors, as a result of invoking validate_vehicle function. the url on the browser displays as MYSITE.com/Customer/validate_vehicle.  

what i want to achieve is  load the view & validation rules within a single function. is this possible? like

 public function new_vehicle()  {

Here i am loading the view

..........



Here i am loading validation stuff.

}

guide me please  .
Reply

#2
The documentation about form validation has this example:

PHP Code:
public function new_vehicle()
{
        
$this->load->helper(array('form''url'));

        
$this->load->library('form_validation');

        if (
$this->form_validation->run() == FALSE)
        {
                
$this->load->view('myform');
        }
        else
        {
                
$this->load->view('formsuccess');
        }

This way, the form is shown if you call the new_vehicle method from your site's navigation, but also if the form doesn't validate because of errors.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.