CodeIgniter Forums

Full Version: Validation of Empty Fields
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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;
        }
    }
}

El Forum

[eluser]ebynum[/eluser]
Upon viewing this again, there is no reason not to simplify this a little more, to:

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

El Forum

[eluser]xwero[/eluser]
i guess you look for something like this
Code:
$rules['dinner'] = 'required';
if(isset($_POST['dinner']))
{
   $rules['meal'] = 'required';
}

El Forum

[eluser]ebynum[/eluser]
That route obviously works - but it is jumping out of validation to do validation.

If I'm going to do inline testing from one rule to the next, what's the point of using validation at all?

El Forum

[eluser]xwero[/eluser]
It doesn't do validation. It simply checks if a value is present and based on that you add other validation rules. maybe the example is too simple to see the difference.

If you are going to build in a rule which isn't a rule but a flag to process the rules it will do just the same.

El Forum

[eluser]xwero[/eluser]
I see what you mean but i think it should be something that should be added
Code:
$rules['dinner'] = 'required';
$rules['meal'] = 'required';
$dependence['meal'] = 'dinner';
$this->set_rules($rules,$dependence);

one rule will not do it for every scenario.

El Forum

[eluser]adamp1[/eluser]
I think there does need to be a rule which will only validate a field if a certain other field is already set. Im always finding I need it.
Code:
$rules['meal'] = "required[dinner]"

Something like that so meal is only required if dinner is set.

El Forum

[eluser]xwero[/eluser]
Should the field only have a value or should it be valid. With a single checkbox it's clear but with a text input it's a bit trickier.
Code:
$rules['email'] = 'required|valid_email';
$rules['confirm_email'] = 'required[email]|valid_email';
If you enter a bad e-mail address and you only check if the field has a value the confirm_email validation doesn't need to run.