![]() |
Form Validation and the bahaviour when the field id 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 and the bahaviour when the field id NOT required (/showthread.php?tid=16469) |
Form Validation and the bahaviour when the field id NOT required - El Forum - 03-07-2009 [eluser]verisof[/eluser] Hi, I'm not sure if that's exactly the bug, but this behaviour really surprised me, because I don't think it's logical. This is the part of original CI_Form_validation class, function _execute: Code: // If the field is blank, but NOT required, no further tests are necessary The situation is: I have two fields (password and its control) but they are not required. If they are left empty, nothing happens, but if not, the control is launched (callback on the first field takes care of it). My rules on those fields are: 1) password_new: 'trim|callback_new_password|xss_clean' 2) password_new2: 'trim|matches[password_new]|xss_clean' Every case works except this one: when I fill the first field and I leave the second empty. That's because FV class doesn't check the "matches" part, because it thinks everything's fine because the field is empty and doesn't have "required" rule. But I don't care if it's empty, I need it to match the first field, every time! Let me know what you think, if this behaviour is logical and I have just a different view. I think it should match other rules although it's empty and not required (at least some of them, like "matches"). Form Validation and the bahaviour when the field id NOT required - El Forum - 03-07-2009 [eluser]Colin Williams[/eluser] So, if password_new is supplied, then password_new2 IS required. So, in code, this looks like: Code: $passnew2rules = 'trim|matches[password_new]|xss_clean'; Quote:Let me know what you think, if this behaviour is logical and I have just a different view. I think it should match other rules although it’s empty and not required (at least some of them, like “matches”). I think there definitely is a case where that should be considered. Form Validation and the bahaviour when the field id NOT required - El Forum - 03-08-2009 [eluser]verisof[/eluser] Yes, I know I can handle it that way or another. But I have the rules in config file and don't want to mess my controller code with them. I still think, that FV shouldn't leave all the other rules out just because the field is empty and not required. You know, when I add the rule "matches", I expect that when those two fields don't match each other, the error will appear. Simple. Form Validation and the bahaviour when the field id NOT required - El Forum - 07-16-2010 [eluser]Unknown[/eluser] I agree with verisof... This seems like a bug to me. I have all my rules in a config file, and would expect that matches[password] would throw an error if password is entered and password2 is left blank. Instead, form validation succeeds and the password is changed without confirming. (Not good!) My config file looks something like this: Code: $config = array( Form Validation and the bahaviour when the field id NOT required - El Forum - 07-16-2010 [eluser]WanWizard[/eluser] I don't think this is a bug. Both password and password2 are not required, so it's perfectly fine to leave them blank. Rules only run when there is something to validate. What you want is required to be conditional based on the value of some other field. For that, you have to extend the form validation library and modify the _execute() method. Or use a callback. There's an example of a 'required_if' callback here. |