Welcome Guest, Not a member yet? Register   Sign In
Form Validation Config File Suddenly Only Works for Some Rules
#1

[eluser]Unknown[/eluser]
Update: I have found the error here it seems that the external form_validation setting do not work with greater_than[n].

The other fields I've tested seem to work fine. greater_than[n] works in the controller but not in the validation config file. For the purposes of my form I was able to replace it with is_natural_no_zero but I could image this would present issues in other forms!

Hi,

So I'm working on a project with numerous form and I've been working with a form validation config file but I'm having some issues.

The current form constantly gets the error:

"Unable to access an error message corresponding to your field name."

To be clear I am not using any custom callback on this form. The strange thing is if the only option in the form validation config is "Required" the error message shows up fine but as soon as I start adding extra parameters it shows that error.

I've stripped down the code to the simplest state to isolate this issue yet it still occurs.

Any help would be greatly appreciated.

This is truly mind boggling me since I'm fairly certain I've done everything correctly. Has anyone experienced anything similar? Also I should note that the form validation works great as soon as I copy it into the same document it just breaks if it's in the config file.

Here is the state I'm currently testing in:

View (the form)
Code:
<?php $this->load->view('header'); ?>
<form id="addclientone" class="uniform" method="post" action="<?php echo base_url(); ?>add_client/add/one"  >
                    <fieldset>
                        <legend>
                            General Details
                        </legend>
                        
                       &lt;?php echo validation_errors(); ?&gt;
                        <dl class="inline">
                        <dt><label for="number_of_clients">Number of Purchasers *</label></dt>
                            <dd>
                                <select name="number_of_clients" id="number_of_clients" class="small">
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                </select>
                            </dd>
                            
                            <dt><label for="condo_id">Select Community *</label></dt>
                            <dd>
                                 <select name="condo_id" id="condo_id" class="small">
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                </select>
                            </dd>
                            
                        </dl>
                          <div class="buttons">
                            <button type="submit" class="button">Begin</button>
                          
                        </div>
                    </fieldset>
                    &lt;/form&gt;
&lt;?php $this->load->view('footer'); ?&gt;

Controller
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Add extends CI_Controller {
    
    public function index()
    {
            $this->load->helper('form');
            $this->load->view('admin/alt_client');
    }
    
    function one() {
    
        $this->load->library('form_validation');
        
        $this->form_validation->set_error_delimiters('<div class="msg error">', '</div>');
        
        $post = $this->input->post();
            
            
        if($this->form_validation->run('clientone') == FALSE)
        {
            
            echo '&lt;html&gt;&lt;body><p>Here they are</p><p>';
            
            foreach($post as $k => $v) //this echos back everything fine
            {    
                echo $k;
                echo "<br>";
                echo $this->input->post($k);
                echo "<br><br>";
            }
            
            
            echo validation_errors();
            echo '</p>&lt;/body&gt;&lt;/html>';
        } else {
        
            //do this later
        }
        
            
    }

}

Form validation config (application/config/form_validation.php)

Please note I removed the extra forms due to their length but I have tested this config file like it is below.
Code:
$config = array(    
                
                        'clientone' => array(
                                    array(
                                            'field' => 'condo_id',
                                            'label' => 'Community',
                                            'rules' => 'trim|required|numeric|greater_than[0]|xss_clean'
                                         ),
                                    array(
                                            'field' => 'number_of_clients',
                                            'label' => 'Number of Clients',
                                            'rules' => 'trim|required|xss_clean' //this works but as soon as I add numeric back in it breaks!!!
                                         )    
                                )                  
               );
#2

[eluser]cideveloper[/eluser]
A couple of things.

1) It does work. I used your code and it works fine with the numeric and greater_than. try this real quick if you want. change 'clientone' in the application/config/form_validation.php to 'add/one' and then change

Code:
$this->form_validation->run('clientone')
to
Code:
$this->form_validation->run()

See if that does anything. Also what version of CI are you using?

2) why are you loading your view in index and then submitting to "one" when $this->form_validation->run('clientone') == FALSE then you should show the view. If it is the first time then you wont have any validation_errors(). When the form is submitted and condition is FALSE again the view is loaded and validation_errors() is available. Basically submitting to itself.
#3

[eluser]Unknown[/eluser]
Thanks for the reply as far as point 2 I do this because it actually is a multipart form I had significantly simplified the code for testing purposes, which eventually reveal the greater_than[n] error as when I was originally running it with more simple validation my multipart form worked fine but stopped suddenly when I put in all the validation. It originally stored session data and the form values of the subsequent pages are created based on their input in step one. Thus if the form is successfully I must load a new view and if it's unsuccessfully I redirect back and echo the errors via flash data. Hence why this is required.

As far as the first point I did just attempt this and I do still get the same error. This is a flawed approached as well since I have multiple forms that are similar enough they can use the same validation config but slightly different enough they need to be process separately. It's not applicable to this form but the application has validation config for client address update that uses the same validation as when it's added initially also I have multiple logins for administrators and salespeople which can access extremely similar forms but with some small differences in permissions thus for the majority of forms I would need to use the same config repeatedly.

Did you test going to a new page? That may be part of it...once again I tried to strip everything to the simplest form to isolate the problem but it does really make me wonder why it is that the greater_than[n] only works in the controller for me. It should really work regardless of the above mentioned factors should it not?

I'm running the latest version of codeigniter downloaded maybe 3 weeks ago..that is most recent right?




Theme © iAndrew 2016 - Forum software by © MyBB