• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Validation: "matches" and NOT required

#1
[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 ?

#2
[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/

#3
[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
}

#4
[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]');

#5
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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