php Codeigniter 中的选择框验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7721621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:14:23  来源:igfitidea点击:

select box validation in Codeigniter

phpcodeignitercodeigniter-form-helper

提问by S.Sid

i am new to Codeigniter and I have some trouble in select box validation. I want default select value at beginning.

我是 Codeigniter 的新手,我在选择框验证方面遇到了一些麻烦。我想在开始时使用默认选择值。

<select name="schooGroups[]">
<option value="0">Select User Group</option>
<option value="1">Admin</option>
</select>

how can i make it required field in form and display error message on select of zero "0" value.

我怎样才能使它成为表单中的必填字段并在选择零“0”值时显示错误消息。

回答by Damien Pirsy

This will have the entry marked as "selected" as...selected by default. You want a multi-select dropdown, right?

这会将条目标记为“已选择”为...默认情况下已选择。你想要一个多选下拉菜单,对吗?

<select name="schoolGroups[]" multiple="multiple">
<option value="0" selected="selected">Select User Group</option>
<option value="1">Admin</option>
</select>

As for validation, you might want to build your own validation rule:

至于验证,您可能希望构建自己的验证规则:

Your controller's method:

您的控制器的方法:

//...
$this->form_validation->set_rules('schoolGroups','School groups','required|callback_check_default');
$this->form_validation->set_message('check_default', 'You need to select something other than the default');

//...

The add this other method:

添加这个其他方法:

function check_default($array)
{
  foreach($array as $element)
  {
    if($element == '0')
    { 
      return FALSE;
    }
  }
 return TRUE;
}

If you just want a single select (no ability to multiselect then) it's even easier:

如果您只想要一个选择(然后无法进行多选),那就更容易了:

html:

html:

<select name="schoolGroups">
<option value="0" selected="selected">Select User Group</option>
<option value="1">Admin</option>
</select>

Method with validation:

验证方法:

 $this->form_validation->set_rules('schoolGroups','School groups','required|callback_check_default');
  $this->form_validation->set_message('check_default', 'You need to select something other than the default');

Callback:

打回来:

function check_default($post_string)
{
  return $post_string == '0' ? FALSE : TRUE;
}

回答by xamiax

The correct way of doing this is by setting the value of the default option to empty! Then you can use a simple form_validation rule like for a text field:

正确的做法是将默认选项的值设置为空!然后,您可以使用简单的 form_validation 规则,例如用于文本字段:

<select name="schoolGroups">
  <option value="">Select User Group</option>
  <option value="1">Admin</option>
</select>

.

.

$this->form_validation->set_rules('schoolGroups','School groups','required');

回答by mndjk

This one worked for me.

这个对我有用。

THE VIEW

风景

<select name="schoolGroups[]" multiple="multiple">
    <option value="">Select User Group</option>
    <option value="1">Admin</option>
</select>

THE CONTROLLER

控制器

$this->form_validation->set_rules('schoolGroups','School groups','callback_check_default');

CALLBACK

打回来

function select_validate()
{
    $choice = $this->input->post("schoolGroups");
    if(is_null($choice))
    {
        $choice = array();
    }
    $schoolGroups = implode(',', $choice);

    if($schoolGroups != '') {
        return true;
    } else {
        $this->form_validation->set_message('select_validate', 'You need to select a least one element');
        return false;
    }
}