Welcome Guest, Not a member yet? Register   Sign In
Form_Validation : Not checking empty fields...
#1

[eluser]simshaun[/eluser]
I have two pages:
Add User
-Username (required)
-Password (required)
-Confirm Password (required) (must match password)

Edit User
-Username
-Password (optional - system doesnt update password unless something is typed here)
-Confirm Password (must match password)

The problem is with the Edit User page.
confirm_password has this rule: matches[password].

If I enter both a password and a confirmation_password, but they don't match.. it works fine and gives me an error.

However, if I enter a password but don't enter a confirmation_password, it does NOT work.
Basically it's not applying the matches[password] rule because the field is empty.

My "dirty" solution was to prepend a 'required' rule to confirmation_password if password isn't empty. I consider it dirty because I'm using a ternary operator in the array:
Code:
$this->form_validation->set_rules('confirm_password', 'Confirm Password', (($this->input->post('password') != '') ? 'required|' : '').'matches[password]');

1. Is there a more elegant solution to fix this?
2. Is this a bug?
#2

[eluser]Mike Ryan[/eluser]
Hi,

I wrote this to solve a similar problem. If you put "depends_on[password]" in your "password confirmation" rules, the other "password confirmation" rules will not run* if "password" has not been entered.

Hope this helps.

* In fact, they do run, but the errors are not registered. I did it this way so callbacks and other functions still work as normal.
#3

[eluser]simshaun[/eluser]
That would be ok, but I'd like a solution that doesn't involve changing the core.

Would it be possible to extend the library with your solution instead?
#4

[eluser]Mike Ryan[/eluser]
Yes, modifying the core isn't ideal. Converting this to an extension of the core form validation class would be a tidier method, and it's on my to-do list... I'll try and post something this weekend.




Theme © iAndrew 2016 - Forum software by © MyBB