• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation - optional fields + callbacks + other rules

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


Messages In This Thread
Validation - optional fields + callbacks + other rules - by El Forum - 10-03-2007, 04:45 PM
Validation - optional fields + callbacks + other rules - by El Forum - 10-05-2007, 05:42 AM
Validation - optional fields + callbacks + other rules - by El Forum - 10-05-2007, 08:53 AM
Validation - optional fields + callbacks + other rules - by El Forum - 10-16-2007, 06:31 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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