2013-01-18 40 views
0

當驗證失敗並重新加載頁面時,我無法弄清楚如何在CodeIgniter中重新填充生日下拉幫助程序。我正在嘗試使用set_value('field_name')這比我的其他下拉菜單更復雜一點,因爲涉及到三個元素。可here重新填寫生日下拉列表

在此先感謝

我的代碼是

下面是驗證規則

$this->form_validation->set_rules('relation', 'Relation', 'trim|required'); 
$this->form_validation->set_rules('location', 'Location', 'trim|required'); 
$this->form_validation->set_rules('city', 'City', 'trim|required'); 
$this->form_validation->set_rules($birthdate, 'Birthday', 'trim|required'); 
$this->form_validation->set_rules('gender', 'Gender', 'trim|required'); 
+0

我有一種感覺失去了一些東西。你的驗證規則在哪裏?如果至少將某種驗證規則應用於元素,'set_value()'通常不起作用。 – Brendan

+0

@Brendan我剛加了他們。對不起,忘了把這些放在第一次。 – LightningWrist

回答

1

文檔訴說:

set_select()

如果您使用菜單,該功能允許您顯示所選的菜單項。第一個參數必須包含選擇菜單的名稱,第二個參數必須包含每個項目的值,第三個(可選)參數允許您將項目設置爲默認值(使用布爾值TRUE/FALSE)。

例子:

<select name="myselect"> 
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option> 
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option> 
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option> 
</select> 
2

這裏是我用來驗證生日(並填充的下拉菜單)一個精簡版。僅供參考我在這裏有一些你可能想要刪除的邏輯。

驗證規則:

$this->form_validation->set_rule('birthdate-month','Birthdate Month','required|is_natural_no_zero|greater_than[0]|less_than[13]'); 
$this->form_validation->set_rule('birthdate-day','Birthdate Day','required|is_natural_no_zero|greater_than[0]|less_than[32]'); 
$this->form_validation->set_rule('birthdate-year','Birthdate Year','required|is_natural_no_zero|greater_than[1930]|less_than['.(date("Y") - 18).']'); 

控制器:

if ($this->form_validation->run()) { 
    $birthdate = array(
     $this->form_validation->set_value('birthdate-year'), 
     $this->form_validation->set_value('birthdate-month'), 
     $this->form_validation->set_value('birthdate-day') 
    ); 

    $data = array(
     'birthdate' => implode("-", $birthdate) 
    ); 
} 

// Then pass $data to your model and do whatever you want, i.e.: 
$this->user_model->update_user($data); 

然後,PHP/HTML視圖:

<select name="birthdate-month" id="birthdate-month"> 
    <option value=""></option> 
    <?php foreach($months as $month_number => $month_name) { ?> 
    <option value="<?php echo $month_number; ?>" <?php echo set_select('birthdate-month', $month_number); ?>><?php echo $month_name; ?></option> 
    <?php } ?> 
</select> 

<select name="birthdate-day" id="birthdate-day"> 
    <option value=""></option> 
    <?php for($i=1; $i<=31; $i++) { ?> 
    <option value="<?php echo $i; ?>" <?php echo set_select('birthdate-day', $i); ?>><?php echo $i; ?></option> 
    <?php } ?> 
</select> 

<select name="birthdate-year" id="birthdate-year"> 
    <option value=""></option> 
    <?php for($i=(date("Y") - 18); $i>=1930; $i--) { ?> 
    <option value="<?php echo $i; ?>" <?php echo set_select('birthdate-year', $i); ?>><?php echo $i; ?></option> 
    <?php } ?> 
</select> 

此外FYI,$months是一個數組:

array(
    '01' => 'January', 
    '02' => 'February', 
    '03' => 'March', 
    '04' => 'April', 
    '05' => 'May', 
    '06' => 'June', 
    '07' => 'July', 
    '08' => 'August', 
    '09' => 'September', 
    '10' => 'October', 
    '11' => 'November', 
    '12' => 'December' 
);