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>

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.