CodeIgniter设置表单验证重定向URL [英] CodeIgniter Set Form Validation Redirect URL

查看:82
本文介绍了CodeIgniter设置表单验证重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当表单没有运行,因为一些字段缺少表单验证器重定向回到当前控制器/方法默认情况下。这意味着如果我为路由到auth / register的'signup'设置了路由,我想它重定向回到路由路径而不是实际的控制器路径。

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.

有任何建议吗?

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);
        }
    }


推荐答案

I如果表单未提交,或者如果有验证错误,则将默认行为设置为您想要的行为,如果表单成功处理,则重定向。

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.

ie

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设置表单验证重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆