当表单由于某些字段缺失而无法运行时,表单验证程序默认重定向回当前控制器/方法。这意味着如果我为“注册”设置路由到auth / register的路由,我希望它重定向回路径路径而不是实际的控制器路径。
有什么建议吗?
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);
}
}
答案 0 :(得分:2)
如果表单未提交或者存在验证错误,我会将默认行为设置为您想要的,然后如果用户成功处理表单则重定向。
即
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);
}
这可能要求您现在改变构建某些内容的方式,但我发现这是处理这种情况的最简单方法。