Welcome Guest, Not a member yet? Register   Sign In
Possible form validation bug?
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone. I've been fighting with the form validation library for most of today. I've been trying to purify my HTML after calling a custom validation callback. In my mind, it should work, but in actuality, it doesn't. I've designed a simple test to illustrate what I'm talking about:

./system/application/controllers/test_form_validation
Code:
<?php

class Test_form_validation extends Controller
{
    
    function index()
    {
        // Load the form validation library.
        $this->load->library('form_validation');
        
        // Set the rules.
        $this->form_validation->set_rules('input', '', 'ltrim|callback__my_callback|rtrim');
        
        // Run validation.
        $this->form_validation->run();
        
        // Output the page.
        $this->output->set_output($this->get_view(TRUE));
    }
    
    function _my_callback()
    {
        // We'll just return TRUE so the test passes.
        return TRUE;
    }
    
    function get_view()
    {

        return <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Form validation test&lt;/title&gt;
        &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;

    &lt;/head&gt;
    &lt;body&gt;
        <div>
            <code>
                content: "{$this->input->post('input')}"
            </code>
        </div>
        &lt;form action="" method="post"&gt;
            <div>
                <label>Input:
                    &lt;input type="text" name="input" value=" some text " /&gt;
                </label>
            </div>
            <div>
                &lt;input type="submit" name="submit" value="Submit" /&gt;
            </div>
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;
EOT;
    }
}

In the case of the above, rtrim is never called. This is fixed if I make the field required, but this isn't a required field. Is this a bug, or am I missing something totally obvious here?
#2

[eluser]Chad Fulton[/eluser]
This appears to be neither something you missed nor a bug, but in fact expected behavior (although stupid behavior it seems to be).

Check out lines 601-605 of Form_validation.php:
Code:
// If the field isn't required and we just processed a callback we'll move on...
if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
{
    return;
}

Apparently, if the field isn't required and a callback was processed, they finish processing right after that. Or maybe they meant to put continue instead of return, and it is a bug.

So, I suppose you could just replace that return with a continue.
#3

[eluser]slowgary[/eluser]
Is it only when using a callback function? I think the validation rules run in succession, maybe the ltrim is happening just not the rtrim? It may be that your callback should be the last validation rule.

Oh... wait a minute... I figured it out. You're missing your constructor ;-P
#4

[eluser]Colin Williams[/eluser]
Quote:Oh… wait a minute… I figured it out. You’re missing your constructor

So. It isn't required.
#5

[eluser]slowgary[/eluser]
Oh. How was I supposed to know?
#6

[eluser]Colin Williams[/eluser]
Basic PHP OOP.
#7

[eluser]slowgary[/eluser]
PHP?
#8

[eluser]TheFuzzy0ne[/eluser]
Thanks, Chad. I think you're right. continue makes much more sense than return.

Gary, you're right. Validation rules do run in succession, but it shouldn't stop processing the rules unless one fails. Smile Also, I'm not sure if you were joking or being serious (can never tell one from t'other with you), but if no constructor is provided, the parent constructor is called automatically. You only need to add a constructor if you want to extend it.

This behaviour is only triggered when you call a custom callback. As a quick and dirty workaround, I've modified my custom callback to purify the data too, so in this case, it doesn't matter about any following rules, since I've put them all before my callback, and the purifying is the last thing that happens.

Thanks for the replies everyone. Should I report this as a bug?
#9

[eluser]slowgary[/eluser]
It sounds like a bug to me, but what do I know. The user guide says:
Quote:You can also process the form data that is passed to your callback and return it.
If your callback returns anything other than a boolean TRUE/FALSE it is assumed
that the data is your newly processed form data.
I take that to say that if your callback returns a non-boolean then it ignores the other validation rules as it assumes you've already processed the data. That means the opposite should be true - a boolean return should continue the validation UNLESS it returns FALSE in which case it would have failed that 'validation rule' and would bark an error. In your case you are returning TRUE, which means that the data has passed your custom validation and should be tested against the others sequentially.

The only part that's a bit questionable is that in the examples they do not combine native validation rules with callbacks. Also, they don't call them "Custom Validation Functions". The very name "callback" makes me suspect that they don't treat it as a normal validation rule. The user guide says form validation accepts any native PHP function that takes 1 parameter. Will it also accept custom functions with 1 parameter? Maybe the "callback_" prefix is only necessary in the event that your function may return prepared data instead of TRUE of FALSE. Have you tried removing the "callback_" prefix from your example code?




Theme © iAndrew 2016 - Forum software by © MyBB