0

当验证失败并且页面重新加载时,我无法弄清楚如何在 CodeIgniter 中重新填充我的生日下拉助手。我正在尝试使用set_value('field_name')它比我的其他下拉菜单更复杂一些,因为它涉及三个元素。

我的代码在这里可用

提前致谢

这是验证规则

$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');
4

2 回答 2

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>

仅供参考,$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'
);
于 2013-01-18T01:49:41.617 回答
1

文档告诉:

设置选择()

如果您使用菜单,此功能允许您显示已选择的菜单项。第一个参数必须包含选择菜单的名称,第二个参数必须包含每个项目的值,第三个(可选)参数允许您将项目设置为默认值(使用布尔值 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>
于 2013-01-18T01:32:51.077 回答