Welcome Guest, Not a member yet? Register   Sign In
validation needs trim for repopulating??
#1

[eluser]frenzal[/eluser]
I've discovered this weird validation issue: the repopulating doesnt work unlessI add trim to the rules as well. I found the solution by comparing to a form that did work, however I find this rather weird behaviour. Any idea why this won't work without trim

Here's the code that doesnt work, tested on 1.61 and 1.5 something

Code:
function test(){
        $this->load->view("test");
    }
    
    function testpost(){
        $this->load->library("validation");
        $rules["test"] = "required";
        $rules["second"] = "required";
        
        $this->validation->set_rules($rules);
        $this->validation->set_error_delimiters('<div class="errMsg">', '</div>');
        
            
        if ($this->validation->run() == FALSE){        
            $this->load->view("test");
        }else{
            echo "ok";
        }
    }

and my form with @ to supress the errors when first viewed
Code:
&lt;?= form_open("admin/mothership/testpost"); ?&gt;
&lt;?= @$this->validation->test_error; ?&gt;
&lt;input type="text" name="test" value="&lt;?= @$this-&gt;validation->test; ?&gt;"/><br />

&lt;?= @$this->validation->second_error; ?&gt;
&lt;input type="text" name="second" value="&lt;?= @$this-&gt;validation->second; ?&gt;"/>
&lt;input type="submit" value="go"/&gt;
&lt;/form&gt;

Bug, feature? Or am i missing something stupid here Smile
#2

[eluser]Tom Glover[/eluser]
I cant see anything wrong with this? so I don't no it may be a bug??
#3

[eluser]Daniel Eriksson[/eluser]
You forgot to set_fields(). Something like this:

Code:
$fields['test'] = 'Test field';
$fields['second'] = 'Second test field';
$this->validation->set_fields($fields);

Check the validation section of the User Guide for more information.

If you do it the right way you can skip the @ also!

/Daniel
#4

[eluser]xwero[/eluser]
The required rules only trigger if there is no input so there will be no value if you repopulatethe field. Or is it the case if you add a value to one field but not to the other that the field with a value doesn't have a value when you repopulate the form?
#5

[eluser]frenzal[/eluser]
$fields aren't required, but I tried adding them anyway and it makes no difference. And don't worry I've read the userguide plenty Smile

"Or is it the case if you add a value to one field but not to the other that the field with a value doesn’t have a value when you repopulate the form?"

-> yep, and by adding a trim to my rules all is well again but I'd still like to get to the bottom of this
#6

[eluser]Daniel Eriksson[/eluser]
[quote author="frenzal" date="1207504756"]$fields aren't required, but I tried adding them anyway and it makes no difference. And don't worry I've read the userguide plenty Smile[/quote]

They ARE required for automatic repopulation. I suggest you re-read the user guide!

/Daniel
#7

[eluser]Pascal Kriete[/eluser]
Daniel E is right on the money. The reason why it works with trim is because of this line (305) in the Validation class:
Code:
$_POST[$field] = $rule($_POST[$field]);
$this->$field = $_POST[$field];

It repopulates the field for native php functions. The ci wrappers only set the POST, so you need to call set_fields seperately.

I don't really understand the design choice here, but regardless, the user guide is quite clear on this in the Re-populating the form section.
#8

[eluser]frenzal[/eluser]
[quote author="Daniel E" date="1207506419"][quote author="frenzal" date="1207504756"]$fields aren't required, but I tried adding them anyway and it makes no difference. And don't worry I've read the userguide plenty Smile[/quote]

They ARE required for automatic repopulation. I suggest you re-read the user guide!

/Daniel[/quote]

wow, i feel so stupid I can't believe I missed that thanks for clearing that up




Theme © iAndrew 2016 - Forum software by © MyBB