carlg Posted August 17, 2012 Share Posted August 17, 2012 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 https://forums.phpfreaks.com/topic/267206-cant-get-codeignitor-to-hold-form-values-on-error/ Share on other sites More sharing options...
Mahngiel Posted August 17, 2012 Share Posted August 17, 2012 I'm pretty certain the set_value only works if there's a validation rule for the field. 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. Link to comment https://forums.phpfreaks.com/topic/267206-cant-get-codeignitor-to-hold-form-values-on-error/#findComment-1370152 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2012 Share Posted August 17, 2012 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 https://forums.phpfreaks.com/topic/267206-cant-get-codeignitor-to-hold-form-values-on-error/#findComment-1370218 Share on other sites More sharing options...
carlg Posted August 17, 2012 Author Share Posted August 17, 2012 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 https://forums.phpfreaks.com/topic/267206-cant-get-codeignitor-to-hold-form-values-on-error/#findComment-1370228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.