验证CodeIgniter中的表单下拉列表 [英] Validating form dropdown in CodeIgniter

查看:112
本文介绍了验证CodeIgniter中的表单下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter的表单助手和表单验证库来构建我的表单。

I am using CodeIgniter's form helper and form validation library to build my forms. I'm having trouble making the dropdown 'sticky' and also finding appropriate validation rules.

这是我如何填充下垂:

foreach($events as $event){
$options[$event->event_title] = $event->event_title;
}
$firstItem = '<option>Please select one...</option>';
echo form_dropdown('events', $options, '', $firstItem);

这是从存储在数据库中的事件构建选项。表单看起来不错,并正确填充所有字段。

This is building the options from events stored in the database. The form looks fine and is populating all the fields correctly.

然而,当我来提交表单时,下拉列表不会保持选择的值?此外,我应该如何验证它,我想要使它需要,但我还要确保我不要除了第一个选项下拉请选择一个...

Hwoever, when I come to submit the form, the dropdown isn't holding onto the value selected? Also, how should I validate it, I want to make it required but I also want to make sure that I dont except the first option in the dropdown 'Please select one...'

提前感谢。

干杯,
Gaz

Cheers, Gaz

推荐答案

你在视图中这样做了吗?我会告诉你我有处理这个,但它都发生在一个控制器:

Are you doing this in a view? I'll show you have I handle this, but it all happens in a controller:

// 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
其中我的case中的键是一个国家id,值是国家名称。我在我的国家数组的开头有'0'=>'NONE'对,所以用户不能选择一个。如果我想让这个需要像你的情况,我可以设置为'-1'=>'请选择...',它将无法验证,因为-1不是一个自然数。

The form_dropdown() function takes an array that is setup like: $key => $value Where the key in my case is a country id, and the value is the countries name. I have the pair '0' => 'NONE' at the start of my country array, so the user cannot choose one. If i wanted to make this required like your situation, I could set it to '-1' => 'Please select...' and it would not validate, as -1 is not a natural number.

希望我的漫游有帮助!

编辑:

你使用form_dropdown()创建下拉菜单,你要做的是检查来自POST数组的选定值。

Okay, so before you create the dropdown with form_dropdown(), what you'll want to do is check for a selected value from coming from the POST array.

在CI的情况下,你可以使用函数set_value($ input),所以在我的示例形式中,我可能会做类似的操作:

In the case of CI, you can use the function set_value($input), so in my example form I might do something like:

$selected = (!empty(set_value('country'))) ? set_value($country) : '';

form_dropdown('country', $this->Country_model->get_countries_dropdown(), $selected)

现在,下拉列表的选定值将被设置为在最后一个帖子上选择的值。您可能需要检查该值以确保其有效。如果没有选择任何内容,那么您可以将$ selected设置为类似于数据库中当前的值,或者您选择的默认值。

So now the selected value of the dropdown will be set to what was selected on the last post. You might want to check that value to make sure it's valid. If nothing was selected, then you could set $selected as something like the value currently in the database, or a default value you've chosen.

这篇关于验证CodeIgniter中的表单下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆