0

codeigniter 2.1.2 を使用して、パラメーターを受け取るコントローラーを介してフォーム ビューをロードしました。このパラメーターは、フォームに特定のコンテンツをレンダリングするかどうかをビューに伝えるブール値です。

標準フォームの 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']);
    }
}
4

1 に答える 1

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>
于 2012-09-28T22:06:34.683 に答える