set_value () not re-populating
In CodeIgniter, I had faced the form validation problem .
Specifically , the problem is set_value () not re-populating.
I ‘ve found what was the problem to read some of the questions and answers of Stackoverflow.
I was in the wrong position to write a set_value ().
I had put the source code . Try to reference .
This article I look forward to helping with someone 🙂
mistake
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
class Form_test extends CI_Controller { public function index() { $this->load->database(); $this->load->helper(array('url')); $this->load->library('form_validation'); $config = array( array( 'field' => 'username', 'label' => 'User name', 'rules' => 'trim|required|min_length[5]|max_length[20]|is_unique[sc_users.username]', ), array( 'field' => 'password', 'label' => 'Password', 'rules' => 'trim|required|min_length[8]', 'errors' => array( 'required' => '%s is required!', ), ), ); $this->form_validation->set_rules($config); $this->data['username'] = array( 'name' => 'username', 'id' => 'username', 'type' => 'text', 'value' => $this->form_validation->set_value('username'), ); $this->data['password'] = array( 'name' => 'password', 'id' => 'password', 'type' => 'text', 'value' => $this->form_validation->set_value('password'), ); if ($this->form_validation->run() == TRUE) { redirect('form_test/success'); } else { $this->load->view('form_test', $this->data); } } public function success() { $this->load->helper(array('url')); $this->load->view('formsuccess'); } } |
view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<html> <head> <title>My Form</title> </head> <body> <?php echo validation_errors(); ?> <?php echo form_open('form_test'); ?> <h5>Username</h5> <?php echo form_input($username);?> <h5>Password</h5> <?php echo form_input($password);?> <div><?php echo form_submit('submit', 'Submit');?></div> </form> </body> </html> |
correct
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
class Form_test extends CI_Controller { public function index() { $this->load->database(); $this->load->helper(array('url')); $this->load->library('form_validation'); $config = array( array( 'field' => 'username', 'label' => 'User name', 'rules' => 'trim|required|min_length[5]|max_length[20]|is_unique[sc_users.username]', ), array( 'field' => 'password', 'label' => 'Password', 'rules' => 'trim|required|min_length[8]', 'errors' => array( 'required' => '%s is required!', ), ), ); $this->form_validation->set_rules($config); if ($this->form_validation->run() == TRUE) { redirect('form_test/success'); } else { $this->data['username'] = array( 'name' => 'username', 'id' => 'username', 'type' => 'text', 'value' => $this->form_validation->set_value('username'), ); $this->data['password'] = array( 'name' => 'password', 'id' => 'password', 'type' => 'text', 'value' => $this->form_validation->set_value('password'), ); $this->load->view('form_test', $this->data); } } public function success() { $this->load->helper(array('url')); $this->load->view('formsuccess'); } } |
view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<html> <head> <title>My Form</title> </head> <body> <?php echo validation_errors(); ?> <?php echo form_open('form_test'); ?> <h5>Username</h5> <?php echo form_input($username);?> <h5>Password</h5> <?php echo form_input($password);?> <div><?php echo form_submit('submit', 'Submit');?></div> </form> </body> </html> |
コメント