我正在使用CodeIgniter的表单助手和表单验证库来构建我的表单。我很难让下拉列表变得“粘性”,也无法找到合适的验证规则。
这就是我填充下拉列表的方式:
foreach($events as $event){
$options[$event->event_title] = $event->event_title;
}
$firstItem = '<option>Please select one...</option>';
echo form_dropdown('events', $options, '', $firstItem);
这是从存储在数据库中的事件构建选项。表单看起来很好,并且正确地填充了所有字段。
那么,当我提交表单时,下拉列表并没有保留所选的值?另外,我应该如何验证它,我想使它成为必需的,但我也想确保我不排除下拉列表中的第一个选项‘请选择一个...’。
提前谢谢。
干杯,加兹
发布于 2010-03-14 21:50:40
您是否在视图中执行此操作?我将向您展示我可以处理这个问题,但这一切都发生在一个控制器中:
// first, we can set a validation rule for the input 'country' (our dropdown), in this case it is required, and must be a natural number. You can look up more rules in the CI user guide, and you can write your own functions as well and add them to the 3rd parameter here. I believe some native PHP functions can be used as well.
$this->form_validation->set_rules('country', 'Country', 'required|is_natural');
// the form is not valid! we'll enter this block whenever the form validation rules above are not met, as well as when first going to this controller-action.
if ($this->form_validation->run() == FALSE) {
// buid your form, there's some CI functions to help with this that I'm using
$my_form = form_open('user/edit', 'class="superform"')
. form_fieldset()
. '<ol>'
. '<li>'
. form_label('Country<br/>', 'country')
// so here is the dropdown, matching the name given to the validation rule we've set, the second parameter takes an array, which I am grabbing from a model, the last parameter is the 'selected; value, which I am grabbing from some variable, if it's not present the first item in the dropdown will obviously be selected
. form_dropdown('country', $this->Country_model->get_countries_dropdown(), $user->country)
. form_error('country', ' <em>', '</em>'
. form_submit('mysubmit', 'Save', 'class="button"')
. '</li>'
. '</ol>'
. form_fieldset_close()
. form_close()
);
// sending the form variable to my view, where i will simply <?=$my_form?> it
$this->load->view('user_edit', $my_form);
} else {
// form has validated! do something!
}
form_dropdown()函数接受如下设置的数组:$key => $value,在我的例子中,键是一个国家id,值是国家名称。我在country数组的开头有一对'0‘=> 'NONE’,所以用户不能选择一个。如果我想让它像您的情况一样是必需的,我可以将其设置为'-1‘-1\f25 =>’请选择...‘并且它不会验证,因为-1不是自然数。
希望我的漫谈能帮上忙!
编辑:
好的,在使用form_dropdown()创建下拉列表之前,您需要做的是检查来自POST数组的选定值。
对于CI,您可以使用函数set_value($input),因此在我的示例表单中,我可能会这样做:
$selected = (!empty(set_value('country'))) ? set_value($country) : '';
form_dropdown('country', $this->Country_model->get_countries_dropdown(), $selected)
因此,现在下拉列表的选定值将被设置为在上一篇文章中选择的值。您可能希望检查该值以确保其有效。如果没有选择任何内容,那么可以将$selected设置为类似于数据库中当前值的值,或者设置为您选择的默认值。
发布于 2011-01-13 03:39:37
在你的队伍中:
echo form_dropdown('events', $options, '', $firstItem);
只是让它:
echo form_dropdown('events', $options, set_value('events'), $firstItem);
发布于 2011-10-09 21:36:41
就我个人而言,我真的很喜欢在视图中构建我的表单,而不是将演示混合到控制器中。但这只是我的观点。我理解如果是一个很大的列表,就必须预先加载数据,但我不认为你真的需要为一个下拉列表构建整个表单。我也有同样的问题,实际上我做了一些文档挖掘来找出一个好的解决方案。
用于验证选择选项的
为此,我在控制器类中提供了一个自定义方法,如下所示
function select_validate($selectValue)
{
// 'none' is the first option and the text says something like "-Choose one-"
if($selectValue == 'none')
{
$this->form_validation->set_message('select_validate', 'Please pick one.');
return false;
}
else // user picked something
{
return true;
}
}
在index() (或处理数据和加载视图的任何方法)中,为下拉菜单设置验证代码。
$this->form_validation->set_rules('select', 'Select', 'callback_select_validate');
在自定义函数前面加上"callback_“,这是命令式。
如果你使用CI提供的东西,维护用户选择保留选择也是非常简单的。您只需要为选择列表中的每个选项使用set_select()函数。
因此,假设您有一个包含两个选项的下拉列表,以简化此操作。这就是你要做的。
<select name="select">
<option value="none" <?php echo set_select('select', 'none', true);?> >- Select One -</option>
<option value="one" <?php echo set_select('select', 'one');?> >Option one</option>
<option value="two" <?php echo set_select('select', 'two');?> >Option two</option>
</select>
使用这个函数的好处是,它预先选择了第一个“虚拟”选项,然后如果用户选择了一个选项,但没有通过其他验证,那么在表单发布后,该选择将被保留并预先选择,并提示用户更正其他错误。
同样,这可能不会让每个人都满意,但对于使用CodeIgniter进行下拉验证来说,这是一个非常简单的解决方案。
https://stackoverflow.com/questions/2442255
复制