• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Should form_validation only be done if post() is true?

#1
[eluser]Scared[/eluser]
In the CI news tutorial the controller for creating a news item sets up the validation rules and tests to see if they succeed. If they do, it saves the news item and loads the success view, otherwise it reloads the form view and displays the appropriate error messages.

I see a lot of examples like this where the form validation rules are setup and tested near the top of the controller. My question is, shouldn't these rules be setup and tested only if $this->input->post() is true?

To me it seems like overhead creating the validation rules and running them when the form might not have even been submitted yet.

Here's the example taken from the tutorial

Code:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a news item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');

if ($this->form_validation->run() === FALSE)
{
  $this->load->view('templates/header', $data);
  $this->load->view('news/create');
  $this->load->view('templates/footer');

}
else
{
  $this->news_model->set_news();
  $this->load->view('news/success');
}
}

Here's my modified example which I think is more efficient:

Code:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a news item';

if ($this->input->post())
{

  /* Save validation requirements for if the form was submitted, rather than just displayed */
  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('text', 'text', 'required');

  if ($this->form_validation->run() === TRUE)
  {
   $this->news_model->set_news();
   $this->load->view('news/success');
   exit;
  }
}

$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}

Am I over-thinking this?

Thanks

#2
[eluser]CroNiX[/eluser]
No, you're not overthinking it and I do the same. There is no reason to load the validation library, set the rules or run the validation, unless there is data to validate.

I usually do something like...

Code:
$submitted = $this->input->post('submit'); //my submit button

if ($submitted)
{
  $this->load->library('form_validation');
  $this->form_validation->set_rules();

  if ($this->form_validation->run() !== FALSE)
  {
    //passed - maybe redirect, or add flashdata success message
  }
  else
  {
    //failed - add flashdata failure message
  }
}

//load the main view.
$this->load->view();
1) form comes up clean upon initial page load
2) if submitted, load and run validation
3) if errors, show errors and general error message
4) if passes, show success message...possibly redirect. Depends on case.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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