CodeIgniter Forums

Full Version: Form validation & view within a one function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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, 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  

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  .
The documentation about form validation has this example:

PHP Code:
public function new_vehicle()


        if (
$this->form_validation->run() == FALSE)

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.