Welcome Guest, Not a member yet? Register   Sign In
Cleaning out validation
#1

[eluser]Tom Vogt[/eluser]
I have a slightly-but-not-totally odd use case where I need to clean out the field data from validation on success. And I can't find a way to do it.

Here's the scenario:

I have a project list and at the bottom there's an "add new project" form. On error, I obviously want the valid fields to be re-populated, everything's fine there.

But on success, I want the new project added to the project list, and the page displayed with an empty form again, ready for input of the next new item, you see?


So how do I do it?
Essentially, I'm looking for $this->validation->reset() or ->clear() or something.
#2

[eluser]Armchair Samurai[/eluser]
Insert the data into the DB and redirect to whatever URL you need to.
#3

[eluser]Daniel Eriksson[/eluser]
Maybe something like this:

Code:
if ($this->validation->run())
{
  $this->Category_model->add_new_category($this->validation->categoryname);
  $data['categories'] = $this->Category_model->get_categories();
  $this->validation->categoryname = '';
  $this->load->view('addcategory_view', $data);
}
else
{
  $data['categories'] = $this->Category_model->get_categories();
  $this->load->view('addcategory_view', $data);
}

If validation succeeded then insert new category (or project in your case), clear the input and reload the same view.
#4

[eluser]Tom Vogt[/eluser]
Hm, neither makes me entirely happy.

Reload is simple, but it only works where I can code the redirect. If I use the same form in multiple views (I work with sub-views a lot) it gets tricky.

Explicit cleaning means I have to change it every time I add or remove a variable from the form. I was thinking of something a lot more automatic.
#5

[eluser]xwero[/eluser]
Add to MY_Validation.php (create class if you haven't already)
Code:
function clear_values()
{
   $fieldnames = array_keys($this->_rules());
   foreach($fieldnames as $fieldname)
   {
      if(isset($this->$fieldname))
      {
         unset($this->$fieldname);
      }
   }
}
not tested
#6

[eluser]scottzirkel[/eluser]
That was close, but I think he's just trying to clear the values of the form. At least, that's what I was trying to do. I still want the validation to work, just didn't want it to repopulate the 'add new project' form with the already added projects data.

Here's what worked for me, again, in the MY_Validation class:

Code:
function clear_values()
    {
        $fieldnames = array_keys($this->_fields);
        foreach($fieldnames as $fieldname)
        {
            $this->$fieldname = null;            
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB