Reputation: 2101
I have the form_validation library loaded in my controller and the form validation itself is working, but when using set_value() it's not populating the form fields. Here is the code in to controller:
function addUser()
{
$this->form_validation->set_rules('firstName', 'firstname', 'trim|required|max_length[30]');
$this->form_validation->set_rules('surname', 'surname', 'trim|required|max_length[30]');
$this->form_validation->set_rules('emailAddress', 'email address', 'trim|required|valid_email|is_unique[users.email]|max_length[255]');
$this->form_validation->set_rules('password', 'password', 'trim|required|max_length[20]|min_length[5]');
$this->form_validation->set_rules('passwordVerify', 'password verification', 'trim|required|max_length[20]|min_length[5]');
if($this->form_validation->run() === FALSE) {
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/member/register');
} else {
echo 'Passed';
}
}
And here is the code in the view:
<?php echo $this->session->flashdata('formValidationError'); ?>
<form method="POST" action="<?php echo site_url('member/addUser'); ?>">
<fieldset>
<legend>Create a FREE account</legend>
<div>
<label for="firstName">Firstname</label>
<input type="text" name="firstName" value="<?php echo set_value('firstName'); ?>" maxlength="30">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" maxlength="30">
</div>
<div>
<label for="emailAddress">Email Address</label>
<input type="text" name="emailAddress" value="<?php echo set_value('emailAddress'); ?>" maxlength="255">
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" maxlength="20">
</div>
<div>
<label for="passwordVerify">Verify Password</label>
<input type="password" name="passwordVerify" maxlength="20">
</div>
<button type="submit">Register</button>
</fieldset>
</form>
Is there something I am missing? Is the redirect causing the issue?
Upvotes: 2
Views: 4682
Reputation: 1548
i had the same issue with redirect()
, i used $this->session->set_flashdata('field','value')
to store data before redirect, as loading view was not possible in my case, and reused it as value of input tag,
<input type='text' value='<?= $this->session->flashdata('field') ?>' >
Upvotes: 2
Reputation: 20492
It is not working because you are using redirection.
Instead of
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/member/register');
just for testing, try to load the view
$this->load->view('member_register_view');
and you will see.
set_value
requires that the form validation ran in the same context... you lose this context when you redirect.
Upvotes: 7