doubu4406 2012-09-28 21:43
浏览 30
已采纳

Codeigniter表单URI在验证失败时丢失段

Using codeigniter 2.1.2, I have a form view loaded through a controller that takes a parameter. This parameter is a boolean that tells the view whether to render certain content on the form.

the URI of the standard form is: ordering/create/ but if the form should be rendering as a 'holiday' form is: ordering/create/holiday

If the submitted form does not pass validation, the standard form is always shown. How do I get the last segment to persist through validation and form re-population?

Here is my controller function.

public function create($holiday = NULL)
{
    $data['holiday'] = ($holiday != NULL) ? TRUE : FALSE;

    //load the code igniter helpers we'll be using
    $this->load->helper('form');
    $this->load->library('form_validation');

    //set the page title
    $data['title'] = 'Create an Order';

    //validate the form input
    $this->form_validation->set_rules('firstName','First Name','required');
    $this->form_validation->set_rules('lastName','Last Name','required');
    $this->form_validation->set_rules('phoneNumber','Phone Number','required|callback_validate_phoneNumber');
    $this->form_validation->set_rules('emailAddress','Email Address','required|valid_email');               
    $this->form_validation->set_rules('pickupDate','Pickup Date','required|callback_validate_pickupDate');
    $this->form_validation->set_rules('pickupTime','Pickup Time','required');

    //check that at least one product was selected
    $this->load->model('products_model');
    $data['products'] = $this->products_model->get_online_products($data['holiday']);
    $productSelections = array();
    $totalProducts = 0;
    foreach($data['products']->result() as $product)
    {
        $productSelections[$product->productProductSizeId] = $this->input->post('product_'.$product->productProductSizeId);
        $totalProducts += $productSelections[$product->productProductSizeId];
    }

    $pickupDate = new DateTime($this->input->post('pickupDate'));

    //if the form failed validation, or there is no form data yet, send the user back to the form
    if($this->form_validation->run() === FALSE || $totalProducts == 0)
    {
        $this->load->model('calendar_model');
        $data['calendarRules'] = $this->calendar_model->get_rules($data['holiday']);

        $data['productSelections'] = $productSelections;

        if($this->form_validation->run() === TRUE && $totalProducts == 0)
        {
            $data['productsError'] = 'You must order at least one product';
        }

        $this->load->view('templates/header', $data);
        $this->load->view('ordering/create', $data);
        $this->load->view('templates/footer');
    }
    else
    {
        //save the data to the DB
        $savedData = $this->ordering_model->create_order($data['holiday']);

        //send the confirmation email and redirect to the confirmation page
        $this->send_confirmation($savedData['orderHeader']['orderId']);
    }
}
  • 写回答

1条回答 默认 最新

  • dszdiavv474681 2012-09-28 22:06
    关注

    In controller ordering, create a method create($holiday = null).

    Then you do this:

    $data['holiday'] = ($holiday != NULL) ? true : false;
    $this->load->view('yourView', $data);
    

    To persist the value of holiday you should set the form submit action accordingly.

    In your view, you could do something like this:

    <?php
    $submitUrl = site_url('ordering/create');
    if ($holiday) {
        $submitUrl .= '/holiday';
    }
    
    <form method="post" action="<?= $submitUrl ?>">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料