• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Process & Validating forms - best practices??

#1
[eluser]Unknown[/eluser]
Hello all,
I'm new to CI and working with MVC setups.

I have a question about the best way to handle processing a form.
I have seen several different methods and am curious what is the best way.

Lets just use a simple 1 field form that we validate, then insert into a database, then do something else with it.

Should you display the view, validate, save it all in the same function?

For example I have my controller :
Code:
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
  
$this->form_validation->set_rules('myfield', 'Test Field', 'required');
  
if ($this->form_validation->run() == FALSE)
{
  $this->load->view('header');
  $this->load->view('form');
  $this->load->view('footer');
}
else
{
  $this->promo_code_model->add_field();
  $this->load->view('success');
}
  
}

or should the processing of the data be handled in a different function in the controller?

In the above example - after I add the field into the database - I then take the input and use it to create a jpg image with it and let the user download it.

Would it be better to do something like this:

Code:
if ($this->form_validation->run() == FALSE)
{
  $this->load->view('header');
  $this->load->view('form');
  $this->load->view('footer');
}
else
{
  $this->promo_code_model->add_field();
  $this->create_jpg_image($this->input->post('myfield'));
  $this->load->view('success');
}

Or in the else part, just do everything in a different function?
Or does it not matter - its kind of whatever floats your boat?

Also - where would the correct place be to put the code that actually handles the JPG manipulation (what I actually do is take that text, and combine it with an image)? Is it ok to put it in the controller, or should it go in the model, or somewhere else?

Thanks
_rob


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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