set_value and validation rules passing


I'm using the Form Validation class and set_value to re-populate form data upon submission. However what I really want is for the form data to only populate when an error is caused from the form validation... if the form is submitted just fine then I don't want the form to populate again.

I was just wondering if this is possible out of the box with CI (i.e. without me creating my own helper function similar to set_value).

For clarification, in this case, the form is for sending a message.. you send the message, the same view reloads with a green message assuming it's been sent ok.. I don't want those form items populated again in this case, only if there has been an error brought up by the validation system.


You can set a flag($flag?$populated_value:"") or clear the $_POST or clear the _field_data from the form validation library.

Ok thanks for your reply Smile

I've tried unset($_POST) to clear that array but it hasn't changed anything. My understanding is that CI may clear this array anyway and store the variables differently?




Just found this topic but didn't help me much:

[eluser]Pale Rider[/eluser]
Mike, did you ever figure out a good solution?

I'm having the same issue. I'm sure it's something simple...

Nope, I haven't found anything yet sorry. I've actually not worked on the project much for the past week or two but at some point in the next month I'll be wanting to find a solution.. like you say I'm sure it's something simple Smile

Ok my solution goes as so...

Create application/libraries/MY_Form_validation.php

Throw this into that file:

<?php class MY_Form_validation extends CI_Form_validation {
    function clear() {
        $this->_field_data = array();
} ?>

Then $this->form_validation->clear(); will clear all data being held from POST/GET etc making set_value() not return the values from the submitted form. So I use this when a form was submitted properly and passes validation, if not of course I want set_value to still work.

Probably not the best way to be doing it just thinking about it now but I threw it together quickly and it works Smile This of course could also be altered to clear a specific variable name if required, or just specific data (post data only, get data only etc).

Hope this helps someone out there.. or at least point them in the right direction of what kind of method could be used.


that was great. thanks so much.

PS- if you use ST-Mike's class, make sure you remove any trailing spaces after the ?> when you paste or you'll get header errors.

[eluser]Dennis Geus[/eluser]
Just what I was looking for.
Works like a charm Wink

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.