Jump to content

Can't get codeignitor to hold form values on error


carlg

Recommended Posts

I'm having a problem getting codeignitor to repopulate the form fields when there is a validation error.  If the validation is successful, the fields are populated properly as expected.

 

Here is the code from my controller which is called by the submit button:

 

 public function addSeller()
        {
            $this->form_validation->set_rules('fname', 'First Name', 'required');

            $data['action'] = site_url('seller/addSeller');
            
            if ($this->form_validation->run() == FALSE)
            {
	$data['message'] = 'fail';
            }
            else
            {
                //SAVE DATA HERE
	$data['message'] = 'success';
            }
            
            $this->load->view('EditSeller', $data);
        }

 

And here is my view:

 

<html>
<head>

</head>
<body>
            <?php echo $message; ?>
            
            <?php echo validation_errors(); ?>
            
    <div class="content">
        <form method="post" action="<?php echo $action; ?>">
        <div class="data">
                    
                    <div>ID:
                        <label><?php echo set_value('id', ''); ?></label>
                    </div>
                    
                    <div>First Name: 
                        <input type="text" name="fname" value="<?php echo set_value('fname', ''); ?>"/>
                    </div>
                    
                    <div>Last Name: 
                        <input type="text" name="lname" value="<?php echo set_value('lname', ''); ?>"/>
                    </div>
                    
                    <div>Email: 
                        <input type="text" name="email" value="<?php echo set_value('email', ''); ?>"/>
                    </div>
                    
                    <div>Password: 
                        <input type="text" name="pword" value="<?php echo set_value('pword', ''); ?>"/>
                    </div>   
          
           <input type="submit" value="Save"/>

        </div>
        </form>
        <br />
    </div>
</body>
</html>

 

Correct. Codeigniter set_value() function ties straight into the form validation class so if you don't have a rule set for the field name the value will never be set. Remember just because a rule exists it does not mean that the field has to be required.

 

i.e here email is not required but the value should be maintained via set_value()

$config = array(array('field' => 'fname', 'label' => 'first name', 'rules' => 'required|trim|xss_filter'),
	   	 array('field' => 'lname', 'label' => 'last name', 'rules' => 'required|trim|xss_filter'),
	 	 array('field' => 'email', 'label' => 'email', 'rules' => 'trim|xss_filter'));
$this->form_validation->set_rules($config);

Thanks for the solution.

 

"On a side-note: why do you wrap your form inputs in a div with a non-contained string?  You should use a label instead."

No reason, this is just a sample project I'm doing for educational purposes.  I was more concerned with learning codeignitor than creating good html at the time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.