CodeIgniter Set Form Validation Redirect URL

2019-06-04 08:37发布

问题:

When the form doesn't get run because some of the fields are missing the form validator redirects back to current controller/method by default. This means that if I've set a route for 'signup' that routes to auth/register I want it to redirect back to the route path not the actual controller path.

Any suggestions?

function register() {
        $this->load->library('form_validation');     
        $this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
        $this->form_validation->set_rules('contact', 'Contact', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
        $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');

        if ($this->form_validation->run() == true) {
                // Do some stuff here
        } else {

            $data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

            $data['name'] = array('name' => 'name',
                'id' => 'name',
                'type' => 'text',
                'value' => $this->form_validation->set_value('name'),
            );
            $data['description'] = array('name' => 'description',
                'id' => 'description',
                'type' => 'textarea',
                'value' => $this->form_validation->set_value('description'),
                'class' => 'form-textarea',
            );
            $data['email'] = array('name' => 'email',
                'id' => 'email',
                'type' => 'text',
                'value' => $this->form_validation->set_value('email'),
            );

            $data['contact'] = array('name' => 'contact',
                'id' => 'contact',
                'type' => 'text',
                'value' => $this->form_validation->set_value('contact'),
            );

            $data['password'] = array('name' => 'password',
                'id' => 'password',
                'type' => 'password',
                'value' => $this->form_validation->set_value('password'),
            );
            $data['password_confirm'] = array('name' => 'password_confirm',
                'id' => 'password_confirm',
                'type' => 'password',
                'value' => $this->form_validation->set_value('password_confirm'),
            );

            $this->load->view('organisations/create', $data);
        }
    }

回答1:

I would set the default behavior to what you want if the form is not submitted or if there are validation errors and then redirect if the user if the form is successfully processed.

i.e.

function register() {
    $this->load->library('form_validation');     
    $this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
    $this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
    $this->form_validation->set_rules('contact', 'Contact', 'required|xss_clean');
    $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
    $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');

    if ($this->form_validation->run()) {
        // Do some stuff here to process the form
        // Maybe set the success flash message
        // Then redirect
        redirect('organizations/login');
    } else {

        $data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

        $data['name'] = array('name' => 'name',
            'id' => 'name',
            'type' => 'text',
            'value' => $this->form_validation->set_value('name'),
        );
        $data['description'] = array('name' => 'description',
            'id' => 'description',
            'type' => 'textarea',
            'value' => $this->form_validation->set_value('description'),
            'class' => 'form-textarea',
        );
        $data['email'] = array('name' => 'email',
            'id' => 'email',
            'type' => 'text',
            'value' => $this->form_validation->set_value('email'),
        );

        $data['contact'] = array('name' => 'contact',
            'id' => 'contact',
            'type' => 'text',
            'value' => $this->form_validation->set_value('contact'),
        );

        $data['password'] = array('name' => 'password',
            'id' => 'password',
            'type' => 'password',
            'value' => $this->form_validation->set_value('password'),
        );
        $data['password_confirm'] = array('name' => 'password_confirm',
            'id' => 'password_confirm',
            'type' => 'password',
            'value' => $this->form_validation->set_value('password_confirm'),
        );
    }

    $this->load->view('organisations/create', $data);
}

This may require you to change the way you structure some things now, but I've found this to be the easiest way to handle this situation.



标签: codeigniter