如何将Yii2的ActiveForm复选框设置为选中状态? [英] How to set Yii2's ActiveForm checkbox in checked state?

查看:324
本文介绍了如何将Yii2的ActiveForm复选框设置为选中状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Yii2 ActiveForm复选框控件的已检查状态的简单解决方案,但找不到如何设置它的解决方案。我在文档中找不到任何示例。

I'm looking for a simple solution for a "checked" state for the Yii2 ActiveForm checkbox control and I can't find a solution how to set it. I cant find any examples in the documentation.

我试图操纵渲染代码

<?= $form->field($model, 'name')->checkbox()->label('Hi'); ?>

但是似乎我需要修改ActiveForm本身。

But it seems I need to modify the ActiveForm itself. How to make checkbox checked by default?

推荐答案

好,我调试了一段时间,找到了解决方案,它位于在ActiveCheckbox()函数中第1359行的BaseHtml.php胆量

Ok, I've debbuged a while and found a solution, it lies in the guts of BaseHtml.php at line 1359 in activeCheckbox() function

$checked = "$value" === "{$options['value']}";

它检查模型变量的默认值:

It checks for the default value of the model variable:

class SomeForm extends Model
{
    public $name = true;

必须将相同的值(具有相同的类型)分配给

And the same value (with the same type) must be assigned to the 'value' option in

<?= $form->field($model, 'name')->checkbox(['value' => true])->label('Hi'); ?>

我会说,这种琐碎的功能过于复杂。

I would say it's overcomplicated as for such trivial feature.

这篇关于如何将Yii2的ActiveForm复选框设置为选中状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆