Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter : Resetting the form values

In my view, what I want to do is clear the form fields once the user has been successfully registered. Everything works fine here i.e. the user is being registered, success message is being shown to the user except that what I want to do is the clear the values of the form fields, for which I am using this

// Clear the form validation field data, so that it doesn't show up in the forms
$this->form_validation->_field_data = array();

After I added this, CI keeps giving me this error: Fatal error: Cannot access protected property CI_Form_validation::$_field_data in

C:\wamp\www\CodeIgniter\application\controllers\user.php on line 92

Here is the relevent controller code:

public function signup()
{
    // If the user is logged in, don't allow him to view this page.
    if (($this->_isLoggedIn()) === true) {
        $this->dashboard();
    }
    else
    {
        $data['page']       = 'signup';
        $data['heading']    = 'Register yourself';
        $data['message']    = $this->_regmsg;

        $this->load->library('form_validation');

        // $this->form_validation->set_rules('is_unique', 'Sorry! This %s has already been taken. Please chose a different one.');

        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]|callback_valid_username');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');

        // run will return true if and only if we have applied some rule all the rules and all of them are satisfied
        if ($this->form_validation->run() == false) {

             $data['errors'] = isset($_POST['submit']) ? true : false;
            $data['success'] = false;

            $this->_load_signup_page($data);
        }
        else{
            if($this->users->register_user($_POST)){
                 $data['errors'] = false;
                $data['success'] = true;

                // Clear the form validation field data, so that it doesn't show up in the forms
                $this->form_validation->_field_data = array();

                $this->_load_signup_page($data);
            }
        }
    }
}

private _load_signup_page($data){
    $this->load->view('template/main_template_head');
    $this->load->view('template/blue_unit', $data);
    $this->load->view('signup', $data);
    $this->load->view('template/main_template_foot');
}

Can anyone please tell me what's the deal with this line?

$this->form_validation->_field_data = array();

P.S: Here is how I am showing the values in the form:

<?php echo set_value('fieldname'); ?>
like image 654
Kamran Ahmed Avatar asked Apr 19 '26 20:04

Kamran Ahmed


1 Answers

It means this is a protected property and you can not use it directly. Instead of this you can do it simply like this

if($this->users->register_user($_POST)){
    $data['errors'] = false;
    $data['success'] = true;

    unset($_POST)

    $this->_load_signup_page($data);
}

This way is not recommended. Instead if you redirect to the same controller the form will reset itself automatically.

if($this->users->register_user($_POST)){
    $data['errors'] = false;
    $data['success'] = true;

    redirect('controllername/signup');
}

Still if you need success messages you can use flash data for this. Here

like image 189
Muhammad Raheel Avatar answered Apr 23 '26 05:04

Muhammad Raheel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!