Php yii2复选框不起作用其始终在POST中提交0

Php yii2复选框不起作用其始终在POST中提交0,php,yii2,crud,Php,Yii2,Crud,我有一个简单的字段在db中的表单tinyint中处于活动状态,当我提交表单复选框选中或未选中时,在规则中也定义为整数。它总是提交0 echo $form->field($model, 'is_active')->checkbox(); 我在处理表单的复选框时使用此选项: $model=(isset($\u POST['is\u active']))?1 : 0; 还要确保没有为复选框设置特定值。否则你会得到奇怪的结果。这个补丁对我很有效,但我仍然想知道为什么这个问题还没有在yii2

我有一个简单的字段
在db中的表单
tinyint
中处于活动状态,当我提交表单复选框选中或未选中时,在规则中也定义为整数。它总是提交0

echo $form->field($model, 'is_active')->checkbox();

我在处理表单的复选框时使用此选项:

$model=(isset($\u POST['is\u active']))?1 : 0;


还要确保没有为复选框设置特定值。否则你会得到奇怪的结果。

这个补丁对我很有效,但我仍然想知道为什么这个问题还没有在yii2平台上报告,或者我在这里做错了什么

diff --git a/vendor/yiisoft/yii2/helpers/BaseHtml.php b/vendor/yiisoft/yii2/helpers/BaseHtml.php
index 66a5730..d2bd49c 100644
--- a/vendor/yiisoft/yii2/helpers/BaseHtml.php
+++ b/vendor/yiisoft/yii2/helpers/BaseHtml.php
@@ -706,10 +706,10 @@ class BaseHtml
      *
      * @return string the generated checkbox tag
      */
-    public static function checkbox($name, $checked = false, $options = [])
+    public static function checkbox($name, $value, $checked = false, $options = [])
     {
         $options['checked'] = (bool) $checked;
-        $value = array_key_exists('value', $options) ? $options['value'] : '1';
+        //$value = array_key_exists('value', $options) ? $options['value'] : '1';
         if (isset($options['uncheck'])) {
             // add a hidden field so that if the checkbox is not selected, it still submits a value
             $hidden = static::hiddenInput($name, $options['uncheck']);
@@ -892,7 +892,7 @@ class BaseHtml
             if ($formatter !== null) {
                 $lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
             } else {
-                $lines[] = static::checkbox($name, $checked, array_merge($itemOptions, [
+                $lines[] = static::checkbox($name, $value, $checked, array_merge($itemOptions, [
                     'value' => $value,
                     'label' => $encode ? static::encode($label) : $label,
                 ]));
@@ -1446,7 +1446,7 @@ class BaseHtml
             $options['id'] = static::getInputId($model, $attribute);
         }

-        return static::checkbox($name, $checked, $options);
+        return static::checkbox($name, $value, $checked, $options);
     }

     /**

这不是一种yii方式,因为z yii还设置了一个隐藏字段来劫持未选中的复选框值。无论出于何种原因,代码在您的安装中无法正常工作,我不知道为什么。我无法复制该问题,但问题似乎不在Yii本身,而是您的特定站点未正确获取复选框值。您的更正正在修复特定于您的问题,因此您可能会通过手动修补内核而遇到可维护性问题,而不是修复导致您的站点无法正确获取复选框值的任何问题。如果它真的是内核中的一个bug,它会影响到每个人,但事实并非如此。我不知道出了什么问题,但还是有一些问题。我将回滚此修补程序并在有时间时尝试找出根本原因。