2012-09-28 56 views
0

使用codeigniter 2.1.2,我通過一個帶有參數的控制器加載表單視圖。該參數是一個布爾值,它告訴視圖是否在表單上呈現特定的內容。驗證失敗時,Codeigniter表單URI丟失段

標準形式的URI是:ordering/create/ 但如果形式應呈現爲一個「節日」的形式是:ordering/create/holiday

如果提交的表單沒有通過驗證,標準的形式始終顯示。我如何通過驗證來持續保留最後一部分並形成重新填充?

這是我的控制器功能。

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

你怎麼驗證? – raidenace

+0

使用form_validation-> set_rules。我在代碼中添加了代碼。 – kscott

回答

1

在控制器ordering,創建一個方法create($holiday = null)

然後你這樣做:

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

要保留的holiday值應設置相應的表單提交操作。

在你看來,你可以做這樣的事情:

<?php 
$submitUrl = site_url('ordering/create'); 
if ($holiday) { 
    $submitUrl .= '/holiday'; 
} 

<form method="post" action="<?= $submitUrl ?>"> 
</form> 
+0

這就是我正在做的。我在代碼中添加了代碼。看別的嗎? – kscott

+0

@kscott:請參閱編輯 – xbonez