I am in the process of creating an input form on a record specific page e.g. (property/rentals/rec_id). On submission, the data should be inserted, validated and return to the original page from which it was sent with any relevant messages.
Echo the specific validation error - e.g. invalid email address etc
If successful, insert data, reload the page and display success message.
function class() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name','name','required|trim');
$this->form_validation->set_rules('email','email','required|trim|valid_email');
$fields['name'] = 'Name';
$fields['email'] = 'Email Address';
$this->validation->set_fields($fields);
if ($this->form_validation->run() == FALSE)
{
//failed - reload page and re-populate fields with any error messages
}
else
{
// display form with success message
}
}
Normally this is simply done by 'loading a view' and passing the relevant data to repopulate the page, fields or display messages. But I have to use a 'redirect' back to the product specific page. Is there any good way to do this other than using session data to carry the validation errors..?
Thanks in advance, Michael
Take a look at the CodeIgniter manual on Sessions and see if 'FlashData' is of any use.
You may use the set_value('field_name')
function to re-populate the form. Sample usage in view:
<input type="text name="name" value="<?php echo set_value('name'); ?>" />"
To display the errors from the form_validation, you may use the validation_errors()
function. Sample usage in view:
<?php echo validation_errors(); ?>
Also, see link text for more info.