我正在构建一个小应用程序,基本上可以显示 3 个表单......第一个是可选的,第二个不是,第三个不是。我希望每个表单都是一个单独的网址(控制器)。在阅读 CodeIgniter form_validation的文档时,表单似乎只能提交给自己进行验证。如果是这种情况,表单将继续显示在同一页面上......他基本上就是我所拥有的...并评论了我想做什么...
class Index extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
//load front page that contains first form...
$content['title'] = 'Home';
$content['view'] = 'pages/index';
$this->load->view('template/default',$content);
}
function step_two()
{
//recieve information from front page. validate form. If validation is
//successful continue to step_two(url) if it fails redirect
//to front page with error...
$this->form_validation->set_rules('serial', 'Serial Number', 'required');
if ($this->form_validation->run() == FALSE)
{
//return to front page...
}else{
//do necessary work and load step_two view
}
}
}
?>
这是我想法的一个片段。但我注意到的是,除非表单提交给它自己,否则您无法进行表单验证。有什么想法吗?我应该验证表单然后重定向到新的 url/函数吗?
谢谢大家...
这就是
你的做法
控制器-
public function step1()
{
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform1');
}
else
{
//do something to post data
redirect('/controller/step2');
}
}
public function step2()
{
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform2');
}
else
{
//do something to post data
redirect('/controller/step3');
}
}
因此,对您的问题的答案是肯定的,您将这些保留在同一方法中,并在成功验证后重定向。