yii2 复选框不起作用,它总是在 POST 中提交 0


yii2 checkbox not working its always submit 0 in POST

>我在表单tinyint数据库中有一个简单的字段is_active,当我提交表单复选框时,它也在规则中定义为整数复选框选中或取消选中它总是提交 0。

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

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

$model= (isset($_POST['is_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);
     }
     /**