Welcome Guest, Not a member yet? Register   Sign In
how to clear all form variables like $this->validation->myvar ?
#1

[eluser]chrisco23[/eluser]
I have an "Add Item" form with a bunch of inputs like:

Code:
<input type="text" name="myvar" value="<?php echo $this->validation->myvar?>" />

I submit this form, and if everything validates, I dump the data into the db. I want to go back to the add form with empty fields -- nothing prepopulated.

Isn't there some simple way to clear all my $this->validation form variables?


Thanks,
Chris
#2

[eluser]wiredesignz[/eluser]
This is a naughty way to do it (using a private variable, tsk tsk) Tongue
Code:
foreach ($this->validation->_fields as $field => $alias)
{
    $this->validation->$field = '';
}
#3

[eluser]chrisco23[/eluser]
heh, thanks. I was close, looking at _fields .... I'll try it.
#4

[eluser]GSV Sleeper Service[/eluser]
if you're using something like

Code:
$this->validation->set_fields($fields);

I'd suggest either not calling validation->set_fields or unsetting $fields before you display the form
#5

[eluser]chrisco23[/eluser]
I've tried both suggested methods now.

My form remains populated.

I must be overlooking something obvious right? I'm not actually a CI newbie (my account got reset when this site relaunched). I've been building CI sites for a couple years!

Just three functions. The index() function calls the addSupplier view. If the data passes validation, I just want to redisplay a clean form. All of the form inputs echo the $this->validation->myvar values.
Code:
function _supplierValidation() {
        // Validation rules
        $rules['name'] = 'trim|required|xss_clean';
        $rules['url'] = 'trim|required|xss_clean|prep_url';
        $rules['about'] = 'trim|required|xss_clean';
        $rules['contact'] = 'trim|required|xss_clean';
        $rules['contactEmail'] = 'trim|required|xss_clean|valid_email';
        $rules['areaCode'] = 'trim|required|integer|xss_clean|exact_length[3]';
        $rules['prefix'] = 'trim|required|integer|xss_clean|exact_length[3]';
        $rules['lastFour'] = 'trim|required|integer|xss_clean|exact_length[4]';
        $this->validation->set_error_delimiters('<div class="form_errors">', '</div>');

        $this->validation->set_rules($rules);

        $fields['type'] = 'Type';
        $fields['name'] = 'Name';
        $fields['url'] = 'Web site';
        $fields['about'] = 'About';
        $fields['contact'] = 'Contact (internal use only)';
        $fields['contactEmail'] = 'Contact email (internal use only)';
        $fields['areaCode'] = 'Contact email (internal use only)';
        $fields['prefix'] = 'Contact email (internal use only)';
        $fields['lastFour'] = 'Contact email (internal use only)';

        $this->validation->set_fields($fields);
    }

    function index() {
        // Page subtitle
        $page['subtitle'] = 'Suppliers';

        $this->_supplierValidation();

        // Page content
        $page['content'] = $this->load->view('themes/' . $this->theme .
            '/admin/addSupplier', NULL, TRUE);

           // Call theme
           $this->load->view('themes/' . $this->theme . '/layout', $page);
    }

    function doadd() {
        // Set validation rules
        $this->_supplierValidation();

        // Perform validation
        if ($this->validation->run()) {
            $supplierData->name = $this->db->escape($_POST['name']);
            $supplierData->url = $this->db->escape(str_replace('http://', '', $_POST['url']));
            $supplierData->about = $this->db->escape($_POST['about']);
            $supplierData->contact = $this->db->escape($_POST['contact']);
            $supplierData->contactEmail = $this->db->escape($_POST['contactEmail']);
            $supplierData->phone = $this->db->escape($_POST['areaCode'] .
                $_POST['prefix'] . $_POST['lastFour']);
            // Insert supplier into db
            $this->SupplierModel->doadd($supplierData);
        }
        // Reset all validation fields
        // Yes, they're private ... wiredesignz suggestion
        foreach ($this->validation->_fields as $field => $alias) {
            $this->validation->$field = '';
        }
        $this->index();
    }
#6

[eluser]brookerrj[/eluser]
I run this after I have successfuly stored the form data in the database.
I am using the later Form Validation class.

Code:
foreach ($_POST as $key => $field)
{
    $this->form_validation->set_rules($key, '');
}

It appears to work well.
#7

[eluser]CroNiX[/eluser]
Havent tried, but does:
Code:
$this->form_validation->set_rules(array());
work?
#8

[eluser]TheFuzzy0ne[/eluser]
The validation function should only be called when the form is being submitted. A simple redirect to the same page after the validation has completed and data has been added to the database should do it. The page will act as it did when it was first called.
#9

[eluser]brookerrj[/eluser]
I am creating some administration pages for maintaining events in a calendar.
I understand what you are suggesting and because it is less kludgy I have changed my events controller code to use the redirects (see simplified code below).

However, as I understand it, a redirect will cause a round trip back to the client even when it's a local redirect (I may well be wrong here, please let me know if I am).

What seems to me more logical and immediate is to go directly to the functions in my controller:

$this->edit($id)
and
$this->add()

It does work for the edit but not for the add because the form data is repopulated.

Code:
function add() {

    // display new event form

}

function edit($id) {

    // display an event in a form for editing

}

function save($id = FALSE) {

    // validate data input and then:

    // for editing an existing event
    if($id) {
        $this->MEvents->saveEvent($event, $id);
        redirect('admin/events/edit/' . $id);  // why not $this->edit($id)
    }
    // for adding a new event
    else {
        $id = $this->MEvents->saveEvent($event, $id);
        redirect('admin/events/add');  // why not $this->add()
    }
}
#10

[eluser]TheFuzzy0ne[/eluser]
Because the post array exists still, and the form will be repopulated with the values again.

Three more ways to do it:

Maybe have another view which doesn't use set_value(), for each of your edit and save pages, and you can load that, although I think that's just a waste of disk space.

The second method may be to set the variables in your controller, use set_value() if the validation hasn't passed, and just set the variable to '', if it did:
Code:
$validation_passed = FALSE;

if ($this->form_validation->run())
{
    $validation_passed = TRUE;
    # Add the data to the database.
}

$data['event_name'] = ($validation_passed) ? '' : set_value('event_name');
$data['event_description'] = ($validation_passed) ? '' : set_value('event_description');

But this would require you to change the structure of your of your code... You'd probably have to set a config item to say that the validation has passed, so your edit and save methods know what to do.

Finally, you could just unset the post array, although that doesn't feel right to me. It may cause some problems, but I doubt it will.




Theme © iAndrew 2016 - Forum software by © MyBB