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
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
<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
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
<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>
コメントを残す