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

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

问题描述

我正在使用 CodeIgniter 的表单助手和表单验证库来构建我的表单.我无法使下拉菜单具有粘性",也无法找到合适的验证规则.

这就是我填充下拉列表的方式:

foreach($events as $event){$options[$event->event_title] = $event->event_title;}$firstItem = '';echo form_dropdown('events', $options, '', $firstItem);

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

但是,当我提交表单时,下拉列表没有保留所选的值?另外,我应该如何验证它,我想让它成为必需,但我也想确保除了下拉列表中的第一个选项请选择一个..."

提前致谢.

干杯,煤气

解决方案

您是在视图中执行此操作吗?我会告诉你我有处理这个问题,但这一切都发生在控制器中:

//首先,我们可以为输入的'country'(我们的下拉列表)设置一个验证规则,在这种情况下它是必需的,并且必须是一个自然数.您可以在 CI 用户指南中查找更多规则,也可以编写自己的函数并将它们添加到此处的第三个参数中.我相信也可以使用一些本机 PHP 函数.$this->form_validation->set_rules('country', 'Country', 'required|is_natural');//表单无效!每当不满足上述表单验证规则时,以及第一次进入此控制器操作时,我们都会进入此块.if ($this->form_validation->run() == FALSE) {//构建你的表单,我正在使用一些 CI 函数来帮助解决这个问题$my_form = form_open('用户/编辑', 'class="superform"').form_fieldset().'

    '.'
  1. '.form_label('国家<br/>', '国家')//所以这里是下拉列表,匹配我们设置的验证规则的名称,第二个参数采用一个数组,我从模型中抓取,最后一个参数是'selected;我从某个变量中获取的值,如果它不存在,则显然会选择下拉列表中的第一项.form_dropdown('country', $this->Country_model->get_countries_dropdown(), $user->country).form_error('国家', ' <em>', '</em>'.form_submit('mysubmit', 'Save', 'class="button"').'</li>'.'</ol>'.form_fieldset_close().form_close());//将表单变量发送到我的视图,我将简单地 <?=$my_form?>它$this->load->view('user_edit', $my_form);} 别的 {//表单已验证!做一点事!}

form_dropdown() 函数采用一个数组,其设置如下:$key => $value在我的情况下,键是国家/地区 ID,值是国家/地区名称.我的国家/地区数组的开头有一对 '0' => 'NONE',因此用户无法选择一个.如果我想像您的情况一样要求这样做,我可以将其设置为-1"=>请选择..."并且它不会验证,因为 -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 设置为类似于当前数据库中的值或您选择的默认值.

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.

This is how I'm populating the drodown:

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...'

Thanks in advance.

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!
}

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.

Hope my rambling helps!

Edit:

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.

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)

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天全站免登陆