dongpei2835 2012-05-03 15:24
浏览 24
已采纳

CodeIgniter验证逻辑

I'm building a small application that will basically display 3 forms... the first one is optional, the second not and the third not. I'd like each form to be a separate url(controller). While reading the documentation for the CodeIgniter form_validation, it appears that the form can only submit to it's self to validate. If that's the case, the forms would keep showing on the same page... he's basically what I have... and commented in what i'd like to do...

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   
          } 


        }

}

?> 

This is a snippet from my idea. But what I'm noticing is that you can't have form validation unless the form submits to it's self. Any ideas? Should I validate the form then just redirect to the new url/function?

Thanks guys...

  • 写回答

1条回答 默认 最新

  • dsqve08622 2012-05-03 15:28
    关注

    this is how you do it

    Controller -

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

    so answer to your question is yes, you keep these in the same method and redirect on successful validation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写