• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form_validation class fails to locate rule-sets for certain URIs

#1
[eluser]sholsinger[/eluser]
In 1.7.0 the Form_validation::run() method will fail to match rule-sets within the config/form_validation.php if the routed URI has a third segment. The matching function merely does a trim($uri, '/'); to check if the uri matches a key in the $config array.

Code:
// Is there a validation rule for the particular URI being accessed?
$uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
            
if ($uri != '' AND isset($this->_config_rules[$uri]))
{
  $this->set_rules($this->_config_rules[$uri]);
}
else
{
  $this->set_rules($this->_config_rules);
}
    
// We're we able to set the rules correctly?
if (count($this->_field_data) == 0)
{
  log_message('debug', "Unable to find validation rules");
  return FALSE;
}

The above code (which is taken from the 1.7.0 source) will fail to match a rule identified by: 'controller/method' when the uri has a third segment. Example: 'http://www.example.com/controller/method/:num'

Perhaps that line ought to be:

Code:
// Is there a validation rule for the particular URI being accessed?
$uri = ($group == '') ? $this->CI->uri->rsegment(1) . '/' .$this->CI->uri->rsegment(2) : $group;

#2
[eluser]sholsinger[/eluser]
I put the proposed line in place of the 1.7.0 line and tested it across a site that makes extensive use of the Form_validation class and it seems to work reliably. I'd be interested if anyone else would be willing to test it out before it is committed to the version control. Smile

#3
[eluser]Unknown[/eluser]
I realized same problem when I try to have Form_validation class find validation rules set in config file.
It doesn't recognize validation rules set which is named correctly('controller'/'function') because of 3rd segment in the URI.

I modified CI_Form_validation class(line: 296) in my server with same way as Sholdinger.
My application uses bunch of form validation rules too. I'll keep testing the this fix.

Thanks Sholdinger,
BTW, I like your signature. keep doing try and error until success!

#4
[eluser]cfraz[/eluser]
Bump. I just ran across this problem, in version 1.7.1.

It seems like the third uri segment will be commonly used (for example to specify a database record) in the very functions that you are most likely to use form_validation on.

It's easy enough to just specify the rule group explicitly by calling form_validation->run('some_rule_group') but when the User's Guide specifies the automatic method:

Quote:When a rule group is named identically to a controller class/function it will be used automatically when the run() function is invoked from that class/function.

it's a pain to have to track down why it doesn't work as expected. The fix seems straight forward enough to include in the next release.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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