Welcome Guest, Not a member yet? Register   Sign In
Config file form validation rule groups: doesn't work with controller in sub-folder?
#1

[eluser]aidehua[/eluser]
I am trying to use a rule group to set form validation rules in my form_validation config file.

I want the rules to be called automatically based on the Controller class name and function - as described under "Associating a Controller Function with a Rule Group" in the user guide.

The class is called Content. The function is called edit_page().

However, my Controller file (content.php) lives in a sub-folder (/application/controllers/admin/content.php).

According the user guide:
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.

But should I include the sub-folder name as well?

I tried both

Code:
$config = array(
            'content/edit_page' => array(
                                                array(
                                                    'field' => 'heading',
                                                    'label' => 'Heading',
                                                    'rules' => 'min_length[4]',
                                                      ),
                                                )

        );

and

Code:
$config = array(
            'admin/content/edit_page' => array(
                                                array(
                                                    'field' => 'heading',
                                                    'label' => 'Heading',
                                                    'rules' => 'min_length[4]',
                                                      ),
                                                )

        );

Neither of them causes the rules to be loaded automatically when I run the form_validation like this in the controller:

Code:
if ($this->form_validation->run() == FALSE):
// load the form view
endif;

On the other hand, if I do

Code:
$config = array(
            'pageedit' => array(
                                                array(
                                                    'field' => 'heading',
                                                    'label' => 'Heading',
                                                    'rules' => 'min_length[4]',
                                                      ),
                                                )

        );

and then call the rules explicitly in the controller like this:

Code:
if ($this->form_validation->run('pageedit') == FALSE):
// load the form view
endif;

it works fine.

Is it possible to use the automatic-rule-loading feature when your class is in a Controller in sub-folder?

If so, what should you put in the array - 'subfolder/class/function' or 'class/function'?

And since I've tried both - what might I be doing wrong?
#2

[eluser]baba[/eluser]
it is definitely a bug. form_validation is not parsing correctly the uri. the problem appears when you pass a parameter to your controller e.g. http://.../admin_user/edit/23 then form_validation tries to find a rule group named edit/23 and ... fails.
#3

[eluser]WanWizard[/eluser]
The form validation library uses
Code:
trim($this->CI->uri->ruri_string(), '/')
as index for the $config array. What does this evaluate to in your case?
#4

[eluser]Unknown[/eluser]
I have the same issue and have some remarks.
I think this is not working really well. Or I'm not using it correctly Smile

- the detection of validation rule in $config is case sensitive. So although the Router is not (I can use http://myuri/Member/Signup or http://myuri/member/signup and it will use the right controller and method), the validation is. Which means that if I change the casing, I get an error logged as it doesn't find any validation rule.

- "ruri_string()" also includes the method parameter. So it works only with methods without any parameter which is quite limitative.


I'm quite new to CodeIgniter and I don't know well its code and all the user cases, but maybe it will work better if Form_Validation->run() was using router->fetch_class() and router->fetch_method() instead of ruri_string(), no ?




Theme © iAndrew 2016 - Forum software by © MyBB