Welcome Guest, Not a member yet? Register   Sign In
Form Validation Rule Groups errors...
#1

[eluser]tjtyrrell[/eluser]
Please forgive me in advance if I am just missing something. I have no clue what is going wrong.

So I want to have 2 rule groups on a specific page and one or the other will be called based on user input.

In my controller I first declare and set the rules
Code:
$config = array(
    'empty' => array(
            array(
                'field' => 'first_name',
                'label' => 'First Name',
                'rules' => 'trim|max_length[0]'
            )
        ),
    'valid' => array(
            array(
                'field' => 'first_name',
                'label' => 'First Name',
                'rules' => 'trim|required'
            )
        )
    );
$this->form_validation->set_rules($config);

Then I try to call one of the groups:
Code:
$this->form_validation->run('valid');

I've found that the code breaks without warning if you have faulty "rules", but I've tested my rules by creating a $config array without the rule groups and they work (any typos in here are because of trying to edit it in a textarea).

(non-rule grouped $config array)
Code:
$config = array(
    array(
        'field' => 'first_name',
        'label' => 'First Name',
        'rules' => 'required'
        )
);

So:
When setting the rules in an array without declaring "groups" my rules work and the echo validation_errors(); call works when calling the "run" method. However, when creating validation rule groups I am having issues. I am trying to follow the user guide but have been racking my brain for hours trying to figure this out on my own, and now need help.

Also, when var_dumping $errors = validation_errors(); I get an empty string... and when if-else testing on the run sequences, the forms do not pass under any circumstances.

Please note:
1) When trying to call the form_validation run() command with rules I use the rule group name I've created (e.g. $this->form_validation->run('valid'); )
2) Thinking that there might be some name conflicts I renamed the groups to random names and they are still not working
3) Currently I am only making 1 rule, I used to have about 8, but with this issue I reduced it to 1 to try and minimize user-errors.

I hope I've divulged enough information. I'm assuming it's just something small that I am completely overlooking. Thank you in advance for any and all help!
#2

[eluser]toopay[/eluser]
To use group rules, you didnt need to declare it on your controller but simply create a file named form_validation.php in your 'application/config/' folder. In that file you will place an array named $config with your rules, like above.
#3

[eluser]tjtyrrell[/eluser]
Bah! So the only way to get array groups to work is using the external config file? I'm trying to have 2 rule-sets for this one page, I have other pages that have a singular rule-set (for example, I have a user signup page). I was hoping that I could have a group of rule-sets for just this page using this method.

I guess not, thanks for pointing out what I suppose should have been obvious - now that I checked the user guide again to see where it states that this option is only for the form_validation.php config file I see that this is "under" the "Saving Sets of Validation Rules to a Config File". Thus the intention that it's only usable this way.

Thanks again - sorry for the newb post,

TJ
#4

[eluser]InsiteFX[/eluser]
Code:
private $validation_rules = array();

public function __construct()
{
    // Call the parent's controller
    parent::__construct();

    // Set the validation rules
    $this->validation_rules = array(
        array(
            'field' => 'field',
            'label' => 'label',
            'rules' => 'required'
        ),
        array(
            'field' => 'field',
            'label' => 'label',
            'rules' => 'required'
        )
    );
}

public function your_method_name()
{
    // Call form_validation and set form rules
    $this->load->library('form_validation');
    $this->form_validation->set_rules($this->validation_rules);

    // Form_Validation Code here!

}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB