Codeigniter3-FormValidation-validate for boolean始终为;假;

Codeigniter3-FormValidation-validate for boolean始终为;假;,validation,boolean,codeigniter-3,Validation,Boolean,Codeigniter 3,我正在使用codeigniters原生FormValidation库检查JSON输入数据。 我设置了如下验证规则: $this->form_validation->set_rules( 'active', 'myCheckbox', 'required|is_bool' ); 下面是JSON数据 { "data": { "type": "items", "attributes": { "categ

我正在使用codeigniters原生FormValidation库检查JSON输入数据。 我设置了如下验证规则:

$this->form_validation->set_rules(
      'active', 'myCheckbox', 
      'required|is_bool'
);
下面是JSON数据

    {
      "data": {
      "type": "items",
      "attributes": {
        "category": "5",
        "description" : "my description",
        "active": false
       }
      }
    }
问题:规则“必需”在
active
的JSON值为
false
时解释为输入不存在,因此只有
active
的值为
true
时验证才会成功

问题:除了将
active
的值更改为
numeric
1
0
并因此更改大量其他对应的代码之外,还有什么方法可以解决这个问题吗

编辑:尝试
isset
而不是
required
行为不同,但也不令人满意

 $this->form_validation->set_rules(
      'active', 'myCheckbox', 
      'isset|is_bool'
);
编辑第2条:在列表[false,true]中尝试
而不是
is\u bool
的行为正确,但JSON看起来不再正确,因为布尔值必须作为字符串发送

$this->form_validation->set_rules(
      'active', 'myCheckbox', 
      'required|in_list[false,true]'
);
下面是JSON数据

{
  "data": {
  "type": "items",
  "attributes": {
    "category": "5",
    "description" : "my description",
    "active": "false"
   }
  }
}
我也有同样的问题。 我修复了它,创建了我自己的“必需”和我自己的“is_bool”。 我创建了一个“is_required_not_null”和“is_boolean”函数,然后放在我的助手中,这样我就可以在需要的地方使用它。 请注意,我使用运算符(==)来确保变量类型和值。 见下文

帮助文件: /yourApplication/helpers/yourHelperFileName\u helper.php

function is_boolean($value)
{
  try {
    if ($value === true ||  $value === false)
      return true;
    else
      return false;
  } catch (\Throwable $th) {
    return false;
  }
}

function is_required_not_null($value)
{
  try {
    if ($value === null)
      return false;

    if ($value === true ||  $value === false)
      return true;

    if (is_array($value) && count($value) == 0) {
      return false;
    } else {
      if (trim($value) == "")
        return false;
    }

    return true;
  } catch (\Throwable $th) {
    return false;
  }
}
使用示例:

    $data = $this->input->post(); //yours inputs ...

    $rules = [          
        [
            'field' => 'fieldName',
            'rules' => 'is_required_not_null|is_boolean',
            'label' => 'Field Name',
            'errors' => [
                'is_required_not_null' => 'Field %s is required.',
                'is_boolean' => 'Field %s must be boolean.',
            ],
        ],
    ]; //yours rules ...

    $this->load->library('form_validation');
    $this->form_validation->set_data($data);
    $this->form_validation->set_rules($rules);

    if ($this->form_validation->run()) {
        //Valid Code ...
    }
    else{
        //Not Valid Code ...
    }
来源文件:

我也有同样的问题。 我修复了它,创建了我自己的“必需”和我自己的“is_bool”。 我创建了一个“is_required_not_null”和“is_boolean”函数,然后放在我的助手中,这样我就可以在需要的地方使用它。 请注意,我使用运算符(==)来确保变量类型和值。 见下文

帮助文件: /yourApplication/helpers/yourHelperFileName\u helper.php

function is_boolean($value)
{
  try {
    if ($value === true ||  $value === false)
      return true;
    else
      return false;
  } catch (\Throwable $th) {
    return false;
  }
}

function is_required_not_null($value)
{
  try {
    if ($value === null)
      return false;

    if ($value === true ||  $value === false)
      return true;

    if (is_array($value) && count($value) == 0) {
      return false;
    } else {
      if (trim($value) == "")
        return false;
    }

    return true;
  } catch (\Throwable $th) {
    return false;
  }
}
使用示例:

    $data = $this->input->post(); //yours inputs ...

    $rules = [          
        [
            'field' => 'fieldName',
            'rules' => 'is_required_not_null|is_boolean',
            'label' => 'Field Name',
            'errors' => [
                'is_required_not_null' => 'Field %s is required.',
                'is_boolean' => 'Field %s must be boolean.',
            ],
        ],
    ]; //yours rules ...

    $this->load->library('form_validation');
    $this->form_validation->set_data($data);
    $this->form_validation->set_rules($rules);

    if ($this->form_validation->run()) {
        //Valid Code ...
    }
    else{
        //Not Valid Code ...
    }
来源文件:


设置规则中
是什么?根据Codeigniter用户指南,您在用户guid@M.Hemant中的什么地方找到了这个
is_bool
:您还可以使用允许最多两个参数的任何本机PHP函数,其中至少需要一个参数(传递字段数据)<“代码>是”bool
是确定的选项之一,所以您是否尝试验证复选框。如果未选中该复选框,则希望抛出验证错误。我说的对吗?不对。我想检查JSON字段
活动
是否已设置->因此
必需
,并确保它是布尔值->因此
是布尔值
设置规则
中什么是
是布尔值?根据Codeigniter用户指南,您在用户guid@M.Hemant中的什么地方找到了这个
is_bool
:您还可以使用允许最多两个参数的任何本机PHP函数,其中至少需要一个参数(传递字段数据)<“代码>是”bool
是确定的选项之一,所以您是否尝试验证复选框。如果未选中该复选框,则希望抛出验证错误。我说的对吗?不对。我想检查JSON字段
活动
是否已设置->因此
必需
,并确保它是布尔值->因此
为布尔值