【问题标题】:Yii: How to add a simple checkbox in a viewYii:如何在视图中添加一个简单的复选框
【发布时间】:2017-07-31 20:51:35
【问题描述】:

我想使用 Yii2 框架为视图添加一个复选框。 使用 HTML、JavaScript 或 Angular 很容易,但我不明白如何使用 Yii2。

我在名为 _form.php 的视图中有一个用户名输入:

<div class="col-md-6">
    <?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
</div>

现在我需要一个复选框。

是否有任何 Yii 文档显示其所有组件?

这是我的模型:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
            [['status', 'created_at', 'updated_at'], 'date','format' => 'd-M-yyyy H:m'],
            [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
            [['username'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'Idadm',
            'username' => 'Nombre de usuario',
            'auth_key' => 'Auth Key',
            'password_hash' => 'Password Hash',
            'password_reset_token' => 'Password Reset Token',
            'email' => 'Email',
            'status' => 'Status',
            'created_at' => 'Creado',
            'updated_at' => 'Actualizado',
        ];
    }
}

所以属性是:username、status、auth_key、email 和password_reset_token。但我想使用一个名为 population 的新属性,但我不知道该怎么做。

【问题讨论】:

    标签: php html checkbox yii2 yii2-advanced-app


    【解决方案1】:

    使用-&gt;checkBox 代替 textInput

    <div class="col-md-6">
       <?= $form->field($model, 'username')->checkBox(['label' => 'your_label']) ?>
    </div>
    

    http://www.bsourcecode.com/yiiframework2/yii2-0-activeform-input-fields/

    http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html

    http://www.yiiframework.com/doc-2.0/guide-helper-html.html

    【讨论】:

    • 我使用 = $form->field($model, 'population')->checkbox(); ?> 但我收到错误“获取未知属性:app\models\User::population”我应该在哪里定义人口?
    • 似乎您的用户模型中没有属性填充... uodate 您提出问题并告诉我您的用户模型
    猜你喜欢
    • 2013-09-01
    • 2018-03-25
    • 2017-12-23
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多