CodeIgniter Forums
Form Validation and a Edit Form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Form Validation and a Edit Form (/showthread.php?tid=50633)



Form Validation and a Edit Form - El Forum - 04-02-2012

[eluser]Glazz[/eluser]
Hello,

I have a form where i have a lot of inputs that goes from the customer firstname to the customer password..

Well, i'm using the form validation library to validate my form inputs, it is working but i have one problem and i think the form library does'nt support this..

The password input when editing a customer is not required but if filled need to be validated, how can i do this, if i can... ?

The rule i use when creating the customer is:
Code:
trim|required|min_length[4]|max_length[20]
I know i don't need to use the required, since i'm using the min_length

I can create a callback but there is another way ?



Edit: I found the problem, i guess.

Since i'm using trim, if the input goes empty the value gets '' and not null since the form validation uses is_null it was not working properly...

Code:
if ( ! in_array('required', $rules) AND is_null($postdata))


I think this needs to be changed, since trim is a nice function, at least for me..


Form Validation and a Edit Form - El Forum - 04-02-2012

[eluser]LifeSteala[/eluser]
I get around this by adding a IF statement checking to see if there was any POST data for the password field. If so, apply validation rules.

See below which works for me.

Code:
$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|valid_email');

if ($this->input->post('password'))
{    
  $this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|max_length[32]');
  $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|trim|xss_clean|matches[password]|max_length[32]');
}