CodeIgniter Forums
Validation - optional fields + callbacks + other rules - 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: Validation - optional fields + callbacks + other rules (/showthread.php?tid=3466)



Validation - optional fields + callbacks + other rules - El Forum - 10-03-2007

[eluser]coolfactor[/eluser]
The current Validation class checks to see if a field has the "required" rule. If it doesn't, meaning the field is optional, then it only skips the field if two other conditions exists:

1 there's no "callback" rule
-and-
2) the field doesn't exist in $_POST or the field is empty

Code:
// Is the field required?  If not, if the field is blank  we'll move on to the next test
if ( ! in_array('required', $ex, TRUE) AND strpos($rules, 'callback_') === FALSE)
{
    if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
    {
        continue;
    }
}

The problem with that logic is that if a callback rule does exist *and* other rules exist that don't explicitly check if the field exists or is empty, then validation may fail anyways, even though the field is optional.

Take this set of rules, for example:
Code:
$rules['email_address'] = 'valid_email|callback_check_email_is_unique';

There's no "required" rule, so the field is optional. However, because there is a "callback" rule, that means the "valid_email" rule will be executed. If the field was empty, validation would fail as a result. The valid_email() function doesn't check (and shouldn't have to check) whether the field is empty. An empty string is not a valid email address.

Because callback functions are custom functions created by the developer, they can explicitly add a check for an empty or missing value. Therefore, it is my conclusion that a check for a "callback" rule doesn't belong here. They should not be treated any different than other rules, in my opinion.

Discussion?


Validation - optional fields + callbacks + other rules - El Forum - 10-05-2007

[eluser]Derek Allard[/eluser]
Yes, makes much sense. Have you implemented a code fix Ted? I'd be interested in seeing it.


Validation - optional fields + callbacks + other rules - El Forum - 10-05-2007

[eluser]coolfactor[/eluser]
The fix is simply to remove the check for a callback function. Here's the same code as above, but with the callback removed:

Code:
// Is the field required?  If not, if the field is blank  we'll move on to the next test
if ( ! in_array('required', $ex, TRUE))
{
    if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
    {
        continue;
    }
}

Callbacks are then treated the same as other functions. If the field is optional, they are only called if the field exists in $_POST and has a non-empty value.


Validation - optional fields + callbacks + other rules - El Forum - 10-16-2007

[eluser]Derek Allard[/eluser]
Added. Thanks as always for your contributions.