0
votes

Gah.. I have spent way to long on this, but I believe I have found the problem.

Essentially I have a hidden field which is populated when a user clicks on an image.

It is required that the user has clicked the image but I do not want the generic form error message for a 'required' check with the CI form validation class.

As such I quickly made a image_required function in my extended form validation class, and set a rule such that this rule was applied to the hidden field.

function image_required($str)
    {

    $CI =& get_instance();

    $CI->form_validation->set_message('image_required','Please click the image above.');

    if($str != '')
    {
        return TRUE;
    }
    else
    {
        return FALSE;
        } 
}

If the hidden field was blank no error was being called.

I am led to believe now that this is because CI says this field is empty yet it is not 'required', therefore we will ignore all the other validation rules for the field. Is this correct?

If so how can i go about requiring this field be set but having a custom error message?

bangs head

Thanks

2
is this a library or a helper?, how do you call in in your validation set rules?tomexsans

2 Answers

2
votes

If you look at the source code (v2.1.3) for the '_execute' routine (system/libraries/Form_validation.php) you will see on line 486

     // If the field is blank, but NOT required, no further tests are necessary

So you are correct, it needs to be required and then it will process your rule.

In order to fix it so you can have a non-required blank field that still processes rules, you should override the '_execute' method by creating a file called 'MY_Form_validation.php' in the application/libraries folder (I think, you might need to check exactly how you extend an existing library) and then copy the '_execute' method and alter the code to continue on a non-required but blank entry.

0
votes

I do love CI, but I have to say this does not allow the flexibility required. It is perfectly reasonable to have a field that cannot be empty, but is NOT required. As in, you wouldn't enforce "user MUST enter a value", but they cannot submit a blank. I think someone got confused between EMPTY and REQUIRED.

1) REQUIRED: User MUST put a value in the field and it cannot be empty (i.e. '') 2) EMPTY: User does not HAVE to enter a value, BUT, if they do, it's cannot be empty. This not the same as REQUIRED... Looks like I'll be using a callback again.

REQUIRED incorporates two logical steps (1->Must enter a value, and 2->Cannot be empty) these two steps should be separated logically to allow either / or.

In constraint terms it would be either, REQUIRED, NOT NULL. Or NOT REQUIRED, NOT NULL.