• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation callables combined with "required"

#1
When I'm using the form validation Callables feature, if combined with Cascaded rules ,and specifically leaving out "required", my callable function is still run and returning FALSE, causing the form validation to return FALSE when run .

In the docs the callables feature is specifically shown with "required":


PHP Code:
$this->form_validation->set_rules(
 
       'username''Username',
       array(
               'required'// <- even if you leave this out, the callable function may return FALSE.
               function($value)
               {
                       // Check $value
               }
       )
); 

In my own validation rules, I am doing this to get around this issue:


PHP Code:
[
    'field' => 'passwd',
    'label' => 'PASSWORD',
    'rules' => [
        'trim',
        
            '_check_password_strength'
            function( $str ) use ( $CI 
            {
                return empty( $str )
                    TRUE
                    
$CI->validation_callables->_check_password_strength$str );
            }
        ]
    ]
], 


But since the docs show that the callables feature is used in conjunction with "required", I am wondering if I am missing something? Ideally, if I leave out "required", and if there is no value to validate, the callable function would not run and cause validation to fail. That desired behavior is just the way form validation behaves when these callable functions are not used.
Reply

#2
Crickets, eh?
Reply

#3
(02-06-2016, 09:06 AM)skunkbad Wrote: But since the docs show that the callables feature is used in conjunction with "required", I am wondering if I am missing something? Ideally, if I leave out "required", and if there is no value to validate, the callable function would not run and cause validation to fail. That desired behavior is just the way form validation behaves when these callable functions are not used.

I guess I haven't tried it, and am too lazy to set up a test app, but if you don't use required, don't the other rules still run? For example, if you cascade required|in_list[A,B,C], validation will fail if the field isn't filled and also if it doesn't contain A, B, or C. But if you remove the required I'm thinking the validation would still fail the in_list rule.

Do you think better logic would be, if there's no required rule, then don't continue validating and just return true?
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply

#4
(02-08-2016, 05:59 PM)RobertSF Wrote: I guess I haven't tried it, and am too lazy to set up a test app, but if you don't use required, don't the other rules still run? For example, if you cascade required|in_list[A,B,C], validation will fail if the field isn't filled and also if it doesn't contain A, B, or C. But if you remove the required I'm thinking the validation would still fail the in_list rule.

Do you think better logic would be, if there's no required rule, then don't continue validating and just return true?

Well, I guess I'm answering your question, but still want to know why the decision was made for callables (and callbacks) to run, even when "required" is not specified. On line 601 of Form_validation.php, you see this:


PHP Code:
// If the field is blank, but NOT required, no further tests are necessary
$callback FALSE;
if ( ! 
in_array('required'$rules) && ($postdata === NULL OR $postdata === ''))
{
    // Before we bail out, does the rule contain a callback?
    foreach ($rules as &$rule)
    {
        if (is_string($rule))
        {
            if (strncmp($rule'callback_'9) === 0)
            {
                $callback TRUE;
                $rules = array(=> $rule);
                break;
            }
        }
        elseif (is_callable($rule))
        {
            $callback TRUE;
            $rules = array(=> $rule);
            break;
        }
        elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
        {
            $callback TRUE;
            $rules = array(array($rule[0], $rule[1]));
            break;
        }
    }

    if ( ! $callback)
    {
        return;
    }



and so it's easy to see that when a field is not required, because "required" is not specified, then if there is no callback the validation ends. But why? Why should validation continue just because there's a callback (or in my case a callable)? At this point, is this just this way for backward compatibility?
Reply

#5
I can't tell you why it was added but I can tell you it wasn't added for compatibility reason. It first appeared 7 years ago: https://github.com/bcit-ci/CodeIgniter/c...79d350407e
Reply

#6
(02-08-2016, 08:16 PM)siburny Wrote: I can't tell you why it was added but I can tell you it wasn't added for compatibility reason. It first appeared 7 years ago: https://github.com/bcit-ci/CodeIgniter/c...79d350407e

The commit message seems to indicate the behavior was desired. I can only guess that it was so that the callback/callable could be used without any other rules. It would be nice if there was a note in the docs, but I'm guessing that many people don't use callbacks/callables, or else the issue would be spoken of more often.
Reply

#7
Well, it's rare that someone doesn't use 'required' AND uses a callback at the same time, but this has been reported as a bug quite a few times.

I'm thinking of changing it in 3.1.
Reply

#8
If the required rule is not used, your callbacks should assume that an empty value is valid. Otherwise, you would use the required rule, right?

On the other hand, I might have a callback which acts as a conditional required rule. I might check other values, either in the $_POST or the database, then determine that a field is required in this situation, and that the empty field is either required or not required. If the callbacks aren't run unless the required rule is in place, then I can't really do conditionally-required fields based on the other values submitted in a form.

One of my most common uses for this type of validation is when someone selects "Other" in a list of radio buttons or checkboxes, at which point the text input for "Other" becomes a required field.
Reply

#9
(02-09-2016, 02:26 AM)Narf Wrote: Well, it's rare that someone doesn't use 'required' AND uses a callback at the same time, but this has been reported as a bug quite a few times.

I'm thinking of changing it in 3.1.

I'm not calling it a bug, and I do see how mwhitney's comment about a "conditionally required" field would present a legitimate reason to keep things as is. A simple note in the docs would be nice.

(02-09-2016, 07:46 AM)mwhitney Wrote: ...

On the other hand, I might have a callback which acts as a conditional required rule. I might check other values, either in the $_POST or the database, then determine that a field is required in this situation, and that the empty field is either required or not required. If the callbacks aren't run unless the required rule is in place, then I can't really do conditionally-required fields based on the other values submitted in a form.

One of my most common uses for this type of validation is when someone selects "Other" in a list of radio buttons or checkboxes, at which point the text input for "Other" becomes a required field.

I see your point. This is helpful.
Reply

#10
(02-09-2016, 10:08 AM)skunkbad Wrote:
(02-09-2016, 02:26 AM)Narf Wrote: Well, it's rare that someone doesn't use 'required' AND uses a callback at the same time, but this has been reported as a bug quite a few times.

I'm thinking of changing it in 3.1.

I'm not calling it a bug, and I do see how mwhitney's comment about a "conditionally required" field would present a legitimate reason to keep things as is. A simple note in the docs would be nice.

I know you're not calling it a bug (and it's not), but others have ... mwhitney's comment is pretty much the reasoning used behind closing such reports as invalid. Yet, it's obvious that this reasoning is obvious to nobody (myself included), hence why I'm thinking of changing it.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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