CodeIgniter Forums
Form Validation: "matches" and NOT required - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Validation: "matches" and NOT required (/showthread.php?tid=35146)



Form Validation: "matches" and NOT required - El Forum - 10-20-2010

[eluser]MeXHere[/eluser]
I have two fields, the second must match the first but they are not required because I use them only to update data when the user fills in that data.

The problem is that even if the second field is set with the rule "matches" to the first field and the first field is filled in with some data .. the validation passes because the second field is not required ...

:gulp:

I'm just missing something or it's just not working as expected ?


Form Validation: "matches" and NOT required - El Forum - 10-20-2010

[eluser]WanWizard[/eluser]
You haven't missed anything. With the current Form Validation implementation it is not possible.

A search finds http://ellislab.com/forums/viewthread/110793/


Form Validation: "matches" and NOT required - El Forum - 10-20-2010

[eluser]CroNiX[/eluser]
A way to get around that is to create dynamic validation rules, or a custom validation rule.

For dynamic, when creating your rules, do something like:
Code:
$first_field = $this->input->post('field_name');

if($first_field)
{
    //create your validation rule for 2nd field using matches to first field, and make it required
}
else
{
    //regular validation rules for 2nd field
}



Form Validation: "matches" and NOT required - El Forum - 10-20-2010

[eluser]n0xie[/eluser]
Doesn't this work?
Code:
$this->form_validation->set_rules('field1', 'field1', 'matches[field2]');
$this->form_validation->set_rules('field2', 'field2', 'matches[field1]');



Form Validation: "matches" and NOT required - El Forum - 10-21-2010

[eluser]MeXHere[/eluser]
But yes n0xie, It's working in that case; It's only that it says that field1 does not match field2 instead of the way around Smile
I suppose I'll be back to using a custom validation rule Big Grin