I have been dealing with a problem for a while. How can I set the validation errors using redirect in a function? This is the code I have in my controller :
function send()
{
$this->form_validation->set_rules('e-mail', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('cellphone', 'Cellphone Number', 'trim|required|is_natural');
$this->form_validation->set_message('required', '%s is required.');
$this->form_validation->set_message('valid_email', '%s is not a valid Email Address');
$this->form_validation->set_message('is_natural', '%s can only contain numbers.');
$this->form_validation->set_error_delimiters('<li>', '</li>');
if($this->form_validation->run() == FALSE)
{
redirect ('/');
}
else
{
echo '<pre>';
print_r($_POST);
echo '<pre>';
}
}
And this is the code I use in my view file:
<? if (validation_errors())
{
echo '<div id="validation_errors" title="Error:">';
echo '<div class="response-msgs errors ui-corner-all"><span>Errors:</span><br /><ul>';
echo validation_errors();
echo '</ul></div>';
echo '</div>';
}
?>
As per my comment:
First, you need to change your form so it points to the current page, i.e.
current_url()
orsite_url('controller/index')
.When you go to the
index
without posting, it will simply skip the validation. Upon submitting your form, it will run the validation.You can then use the built in
form_error
orvalidation_errors
methods to display the errors within yourindex
view.I know it's a bit late but this method works wonders for me.
If you are validating your form in a different function than the one the form is loaded in, you can send your
validation_errors()
to any page that youredirect()
by passing thevalidation_errors()
method to$this->session->set_flashdata()
like so:In your controller functions where you would like your errors or messages to be received you can then set them to the
$data
array like so:At the top of my views I usually include:
Using twitter bootstrap classes to format the messages helps to differentiate them.
I included the
message
flashdata so that you can see how whatever type of message or error you want to send, you are able to format them differently for all information, warning, success and error messages.I found the way to do it. Redirecting does not keep the data to be shown. I used the code below to solve the problem:
finally i got a solution for the redirect with validation_errors
This is using to pass the validation_errors in the session data, i do it like this
and in the page i used
I wish you like this fast solution