Welcome Guest, Not a member yet? Register   Sign In
Form Validation - Best Practice
#1

[eluser]lachavvy[/eluser]
Hi Guys,

Sorry if this has already been debated before, but I'm in the middle of writing some form validation for my website (www.premieradverts.com) and I have found writing all my form validation in the controller to be too much code so I've put it in a library called validate.

Is this good practice or is this something that shouldn't be done as it is only really going to be used for one controller?

Code:
<?php
class Validate{

function new_owner(){
  $CI =& get_instance();
  $CI->load->library('form_validation');
    
  $CI->form_validation->set_rules('first_name', 'First Name', 'trim|required');
  $CI->form_validation->set_rules('surname', 'Surname', 'trim|required');
  $CI->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean|callback_username_check');
  $CI->form_validation->set_rules('password', 'Password', 'required|min_length[5]|matches[password_confirm]');
  $CI->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
  $CI->form_validation->set_rules('email_address', 'Email', 'required|valid_email');
    
  $CI->form_validation->set_rules('address_line_one', 'Company Address - Line One', 'trim|required');
  $CI->form_validation->set_rules('postcode', 'Company Postcode', 'trim|required|min_length[6]|max_length[9]');
  $CI->form_validation->set_rules('city', 'Company City', 'trim|required');
  $CI->form_validation->set_rules('phone', 'Company Phone Number', 'trim|required');
}
}

This has allowed me to call from my controller

Code:
$this->validate->new_owner();


Rather than the whole block of validation code. It's easier for me to read, but is it good practice? Any help would be greatly appreciated.
#2

[eluser]CroNiX[/eluser]
You can also put your rules in a config file.
http://ellislab.com/codeigniter/user-gui...ngtoconfig
#3

[eluser]Aken[/eluser]
I do all my form validation logic in the controller. You can do it that way if you prefer, there really isn't a specific way you need to do it. As long as you don't add a ton of extra steps when you don't have to (which you aren't).
#4

[eluser]jellysandwich[/eluser]
Models are typically the best place to put validation, especially for reusability. No need to create a separate library imo, and you can even use the same syntax as in controllers ($this->form_validation vs $CI->form_validation).
#5

[eluser]lachavvy[/eluser]
Thanks for the quick replies.

I didn't realise there was a function already within form_validation, which I should have looked at originally. It seems useful, though having to create an array for each rule seems a bit excessive.

I'll continue developing my site using the library but will look into the other methods once I start a new site.

Cheers again Smile
#6

[eluser]Mauricio de Abreu Antunes[/eluser]
Take a look at my library. Simple but works fine:
Code:
class MY_Form_validation extends CI_Form_validation {
  
  /**
   * Valida os dados do form.
   * @access  public
   * @param  string $grupo  
   *    grupo de validação
   * @param  string $delimitador
   *    delimitador das mensagens de erro
   */
  public function valida_dados($grupo, $delimitador) {
   $delimitador = array("<$delimitador>", "</$delimitador>");
   $this->set_error_delimiters($delimitador[0], $delimitador[1]);
   return $this->run($grupo);
  }
  
} // Fim da classe MY_Form_validation

This way, you just need to call $this->form_validation->valida_dados('your_grou_name', 'your_delimiter').




Theme © iAndrew 2016 - Forum software by © MyBB