[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->form_validation->run('signup') == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->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:
<?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>";
}
?>
<form action=<?php echo current_url();?> method="post">
<h1>Budget</h1>
<div class="form_section<?php echo $budget_class?>">
<?php
echo $errors['budget'];
?>
...budget form stuff...
</div>
<h1>Taxes</h1>
<div class="form_section<?php echo $taxes_class?>">
<?php echo $errors['taxes'];
?>
...taxes form stuff...
</div>
</form>
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.