Welcome Guest, Not a member yet? Register   Sign In
form_validation and multiple rulesets
#1

[eluser]johnnybravoh[/eluser]
Hi all,

I have really had a hard time dealing with the form_validation library and how it manages multiple rulesets. Here's an example of what I want to do:

form_validation.php
Code:
$config=array(
   'profile_data'=>array(
       array(
            'field'=>'name',
            'label'=>'Name',
            'rules'=>'required'
        ),
              .
              .
              .
             etc...
       ),
    'login_data'=>array(
          array(
            'field'=>'username',
            'label'=>'User Name',
            'rules'=>'required'
        ),
              .
              .
              .
             etc...
       )
);

controller.php
Code:
$this->form_validation->run('profile_data');
$vars['profile_errs']=validation_errors(); //capture the errors that are associated with the profile section

$this->form_validation->clear_errors();<--something like this which lets me reset errors or something to that effect!!!

$this->form_validation->run('login_data');
$vars['login_errs']=validation_errors(); // capture the errors associated with the login_data section

$this->load->view('vw_form',$vars);

vw_form.php
Code:
&lt;form method="post" action="&lt;?php echo current_url()?&gt;"&gt;

<h2>Profile Section</h2>
&lt;?php echo $profile_errs?&gt;

[i]...profile form...[/i]

<h2>Login Section</h2>
&lt;?php echo $login_errs?&gt;

[i]...username/password form...[/i]

&lt;/form&gt;


In summary, I want to be able to run 2 (or more) different rulesets and save each rulesets error message while everything else about form_validation works as expected. I have looked through many posts but I have yet to see a solution that deals with what I'm looking for. I find it difficult to believe that no one else has had this problem and/or found a solution.

Also, please don't worry about any missed syntax in the example above. Syntax and minor coding errors are NOT the problem. If there is a syntax or logic error above don't try and address it. I threw this example together in a hurry without a lot of checking.

Any ideas?
#2

[eluser]johnnybravoh[/eluser]
bump
#3

[eluser]escape[/eluser]
Did you ever figure this out. I have the same problem.

Thanks
#4

[eluser]InsiteFX[/eluser]
If you read the User's Guide you will see that you are calling the
form_validation wrong!

Calling a Specific Rule Group
In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this:
Code:
if ($this->form_validation->run('signup') == FALSE)
{
   $this->load->view('myform');
}
else
{
   $this->load->view('formsuccess');
}

Associating a Controller Function with a Rule Group
An alternate (and more automatic) method of calling a rule group is to name it according to the controller class/function you intend to use it with. For example, let's say you have a controller named Member and a function named signup. Here's what your class might look like:
Code:
&lt;?php

class Member extends Controller {

   function signup()
   {      
      $this->load->library('form_validation');
            
      if ($this->form_validation->run() == FALSE)
      {
         $this->load->view('myform');
      }
      else
      {
         $this->load->view('formsuccess');
      }
   }
}
?&gt;

You need to re-read the Users Guide!

InsiteFX
#5

[eluser]johnnybravoh[/eluser]
[quote author="InsiteFX" date="1286626975"]If you read the User's Guide you will see that you are calling the
form_validation wrong!

Calling a Specific Rule Group
In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this:
Code:
if ($this-&gt;form_validation-&gt;run('signup') == FALSE)
{
    $this-&gt;load-&gt;view('myform');
}
else
{
    $this-&gt;load-&gt;view('formsuccess');
}
[/quote]
Did you even read the original post? This is exactly what is being done.
Quote:You need to re-read the Users Guide!

InsiteFX

You need to re-read the original post.

At issue here is the fact that it's currently impossible to separate the validation_errors() from multiple rulesets.

I have, however found a workable solution for me. It's a bit hackish, but it works. Here goes:
Lets say you have multiple sections in a single page and you want to display the errors present in each section above each section. Simply use a $config array like specified in the form_validation documentation.

form_validation.php
Code:
$config=
array(
'budget'=>array(
array(
                            'field'=>'inc',
                            'label'=>'Your gross monthly income',
                            'rules'=>'numeric'
                        ),
                        array(
                            'field'=>'inc_ot',
                            'label'=>'Your estimated monthly overtime',
                            'rules'=>'numeric'
                        ),
                        array(
                            'field'=>'inc_spouse',
                            'label'=>'Your spouses gross monthly income',
                            'rules'=>'numeric'
                        ),
)
),
'taxes'=>array(
                            array(
                                'field'=>'federal_tax',
                                'label'=>'Federal Tax',
                                'rules'=>'numeric'
                            ),
                            array(
                                'field'=>'state_tax',
                                'label'=>'State Tax',
                                'rules'=>'numeric'
                            ),
                            array(
                                'field'=>'social_security',
                                'label'=>'Social Security',
                                'rules'=>'numeric'
                            )
)

Then just loop through the rulesets you're interested in and assign the validation errors to a variable used in that section. Afterwards, clear the validation errors and field data.

controller.php
Code:
function _reset_validation(){
        $this->form_validation->_error_array = array();
        $this->form_validation->_field_data = array();
}

                $err_sections=array('budget','taxes');
                
                $this->app_vars['form_errors']=false;
                foreach ($err_sections as $section){
                    $this->app_vars[$section.'_class']="";
                    if (!$this->form_validation->run($section,true)){
                        $this->app_vars['errors'][$section]=validation_errors();
                        $this->app_vars[$section.'_class']=' err';
                        $this->app_vars['form_errors']=true;
                    }
                    $this->_reset_validation();
                }

if (count($this->app_vars['errors'])==0){
//post to db or whatever
}else{
$this->load->view('vw_form_page',$this->app_vars);
}

View app_vars.php
Code:
&lt;?php
if ($form_errors=="true"){
    echo "<h1 class=\"error\">There are errors on this page. Please scroll down and address the issues listed below</h1>";
}
?&gt;
&lt;form action=&lt;?php echo current_url();?&gt; method="post"&gt;

<h1>Budget</h1>
<div class="form_section&lt;?php echo $budget_class?&gt;">
&lt;?php
echo $errors['budget'];
?&gt;  
...budget form stuff...
</div>

<h1>Taxes</h1>
<div class="form_section&lt;?php echo $taxes_class?&gt;">
&lt;?php echo $errors['taxes'];
?&gt;
...taxes form stuff...
</div>
&lt;/form&gt;

I have each section surrounded with a div called form_section - you could easily use a fieldset. the cool thing is that if that form section happens to generate an error, the div class has err appended to it, so in the case of the taxes section with an error it would be <div class="form_section err"> Then you can style the div however you want - e.g. div.err{border:2px solid red;}

Hope that helps.
#6

[eluser]escape[/eluser]
Thanks for the suggestion, however I decided to take another approach. In my situation I had a form where the user could change their username, or username and password together. This required three text field inputs, username, new password and old password. The old password was required for any change, however the new password field could be left blank if they only wanted to change the username.

To side step resetting the validation rules, I decided to implement some control flow based upon the posted content (i.e. $this->input->post('username'), $this->input->post('new_password') & $this->input->post('current_password')), then apply a different form validation rule for each logic branch. Along with validation rules, a couple of callbacks were needed to check the uniqeness of usernames and verify a valid current password.

This whole process was further complicated in that an account administrator could change usernames and passwords WITHOUT knowledge of the user's password.

Again thanks for your reply, I may use it in the future. :-)




Theme © iAndrew 2016 - Forum software by © MyBB