Welcome Guest, Not a member yet? Register   Sign In
Validation of Empty Fields
#1

[eluser]ebynum[/eluser]
Currently, the only validation rule that CodeIgniter will run on an empty (or not set) input is required. If the field is not required, the empty input is simply allowed, and validation moves on to the next input. If the field is required, the emptiness is sufficient to invalidate the input, and other rules are not processed.

As a result, validation rules cannot be used to confirm that specific fields are "allowed" to be left empty (or not).

Consider the following scenario: the user is given a "YES|NO" choice about whether he will be attending dinner and a text input for what he would like to eat. If the user is attending dinner, the meal choice input is required. However, if he is not attending dinner, the meal choice input is not required. There is currently no way to process this in CodeIgniter.

There is a simple fix for this. Create a new native rule called something like force_rules. In the event of an empty input without the required rule, the force_rules directive would cause CodeIgniter to process the rules despite the input being empty. The change to the Validation Class would look like this:

Code:
/************ ORIGINAL ************/
if ( ! in_array(’required’, $ex, TRUE))
{
    if ( ! isset($_POST[$field]) OR $_POST[$field] == ‘’)
    {
        continue;
    }
}

/************   FIX   *************/
if ( ! in_array(’required’, $ex, TRUE))
{
    if ( ! isset($_POST[$field]) OR $_POST[$field] == ‘’)
    {
        if ( ! in_array(’force_rules’, $ex, TRUE))
        {
            continue;
        }
    }
}


Messages In This Thread
Validation of Empty Fields - by El Forum - 03-28-2008, 06:50 PM
Validation of Empty Fields - by El Forum - 03-31-2008, 06:58 AM
Validation of Empty Fields - by El Forum - 03-31-2008, 07:24 AM
Validation of Empty Fields - by El Forum - 03-31-2008, 07:32 AM
Validation of Empty Fields - by El Forum - 03-31-2008, 07:42 AM
Validation of Empty Fields - by El Forum - 03-31-2008, 07:55 AM
Validation of Empty Fields - by El Forum - 03-31-2008, 08:24 AM
Validation of Empty Fields - by El Forum - 03-31-2008, 08:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB