CodeIgniter Forums

Full Version: "Unable to find validation rules" error after redirect
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.
I am trying to implement the post-redirect-get pattern, here's my problem (I think it could be a bug):

post -> redirect (to same controller) -> "Unable to find validation rules"
I perform redirecting using this line of code:
PHP Code:
redirect(current_url(), 'location'302); 

if I don't redirect:

post -> form_validation->run() -> everything works as expected.



forgot to say: i'm using CodeIgniter Version 3.0.1
You post your form to a URL and from that URL you redirect to that same URL? So reloading the page to it's original state?
It's not a bug ... you cannot reasonably expect the framework to somehow maintain an object's state between two separate requests - that's two separate processes.

I suspect you just don't quite understand the PRG pattern and you don't need a redirect in the first place, similarly to this guy here: https://github.com/bcit-ci/CodeIgniter/issues/2903
lol. I love reading your posts Narf, they are so funny in their precision and brevity.

Quote:Precisely - you're in the minority, because it's wrong. Smile

Best wishes,

Paul.
(09-28-2015, 06:46 AM)Narf Wrote: [ -> ]It's not a bug ... you cannot reasonably expect the framework to somehow maintain an object's state between two separate requests - that's two separate processes.

I suspect you just don't quite understand the PRG pattern and you don't need a redirect in the first place, similarly to this guy here: https://github.com/bcit-ci/CodeIgniter/issues/2903

Form validation rules are loaded from the config file everytime .. so I can't understand why it should be unable to find validation rules

Anyway..
1 - If I normally visits the page w/o posting, no error is shown in the logs
2 - If I submit data w/o any redirects everything works (no errors again)

If I submit data then redirect, I would expect that It works as if I didn't post anything

problem is it seems I get that "Unable to find validation rules" only if I redirect after a post.

p.s.
I set csfr protection temporarily off during theese tests just in case

p.p.s
I was doing this just because I hoped i could extend, let's say, Form_validation class, to automatically redirect posts without doing it manually
(09-28-2015, 07:44 AM)PaulD Wrote: [ -> ]lol. I love reading your posts Narf, they are so funny in their precision and brevity.


Quote:Precisely - you're in the minority, because it's wrong. Smile

Best wishes,

Paul.

Yea, I'm not really proud with that one actually ... and let's keep this on topic please.

(09-28-2015, 10:25 AM)federico Wrote: [ -> ]
(09-28-2015, 06:46 AM)Narf Wrote: [ -> ]It's not a bug ... you cannot reasonably expect the framework to somehow maintain an object's state between two separate requests - that's two separate processes.

I suspect you just don't quite understand the PRG pattern and you don't need a redirect in the first place, similarly to this guy here: https://github.com/bcit-ci/CodeIgniter/issues/2903

Form validation rules are loaded from the config file everytime .. so I can't understand why it should be unable to find validation rules

Anyway..
1 - If I normally visits the page w/o posting, no error is shown in the logs
2 - If I submit data w/o any redirects everything works (no errors again)

If I submit data then redirect, I would expect that It works as if I didn't post anything

problem is it seems I get that "Unable to find validation rules" only if I redirect after a post.

p.s.
I set csfr protection temporarily off during theese tests just in case

p.p.s
I was doing this just because I hoped i could extend, let's say, Form_validation class, to automatically redirect posts without doing it manually

Well, for starters - it doesn't know where you're redirecting from.
But that's not the point; I'm not saying it's impossible, I'm saying you shouldn't expect it to do that.

Please post the code if you need help with resolving whatever the problem is ... I'm sure it's just a logical error.
(09-28-2015, 10:25 AM)federico Wrote: [ -> ]Form validation rules are loaded from the config file everytime .. so I can't understand why it should be unable to find validation rules

Anyway..
1 - If I normally visits the page w/o posting, no error is shown in the logs
2 - If I submit data w/o any redirects everything works (no errors again)

If I submit data then redirect, I would expect that It works as if I didn't post anything

problem is it seems I get that "Unable to find validation rules" only if I redirect after a post.

p.s.
I set csfr protection temporarily off during theese tests just in case

p.p.s
I was doing this just because I hoped i could extend, let's say, Form_validation class, to automatically redirect posts without doing it manually

I did a quick search in the form_validation library and found the error message 'Unable to find validation rules'. I think this is just a case of a misleading error message, or a difference in the way the code is checking for the existence of data to validate in two different methods. When you call run():

  1. it attempts to determine whether there is data to process, and, for one reason or another, you're getting past this check (maybe there's something in $_POST so count($validation_array) !== 0, or maybe count() returned 1 because of an error condition).
  2. It counts $this->_field_data, which is set by any number of methods in the library, and if the count is 0, it counts $this->_config_rules, which is set in your case because you're using validation rules from the config file.
  3. It goes through and sets the rules,
  4. then it checks whether $this->_field_data still returns a count of 0, and returns this error message if that is the case.
The reason $this->_field_data still returns a count of 0 is simply because set_rules() returned before building $this->_field_data, which usually means it couldn't find any rules, but can also occur when it can't find any data. One of the reasons run() might get to this point but set_rules() might return without finding any data is that set_rules() does this to check the data:

PHP Code:
    public function set_rules($field$label ''$rules = array(), $errors = array())

    {
        // No reason to set rules if we have no POST data
        // or a validation array has not been specified
        if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
        {
            return $this;
        

while run() just does this:

PHP Code:
    public function run($group '')

    {
        // Do we even have any data to process?  Mm?
        $validation_array = empty($this->validation_data) ? $_POST $this->validation_data;
        if (count($validation_array) === 0)
        {
            return FALSE;
        

So, when you redirect, $this->CI->input->method() !== 'post' and you didn't use set_data(), so $this->validation_data is empty, and set_rules() exits and run() thinks it didn't find any rules, when it really just didn't find any data.
Thank you mwhitney, clear explanation