Yii2如何在屏幕上显示Activeform radioList数据?

pvcm50d1  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(168)

我没有使用Yii2的经验。我希望能够在屏幕上显示一个表单的结果。对于字段名称和电子邮件我没有问题,但我不能显示我在radioList中所做的选择。我尝试了很多方法,但都不起作用。你能帮助我吗?
这些是我的档案
SiteController.php

public function actionEntry()
    {
        $this->layout = 'print';
        $model = new EntryForm();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
          return $this->render('entry-confirm', ['model' => $model]);
        } else {
          return $this->render('entry', ['model' => $model]);
        }
    }

EntryForm.php

namespace app\models;

use Yii;

class EntryForm extends \yii\db\ActiveRecord
{
    public $name;
    public $email;
    public $category;

      public function rules()
        {
        return [
            [['name', 'email'], 'required'],
            ['email', 'email'],

        ];
    }

entry.php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'name') ?>

    <?= $form->field($model, 'email') ?>
    <?= $form->field($model, 'category')->radioList([
        1 => 'radio 1', 
        2 => 'radio 2'
    ]);
     ?>

   <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> 

    </div>

<?php ActiveForm::end(); ?>

entry-confirm.php

<p>You have entered the following information:</p>
<ul>
    <li><label>Name</label>: <?= Html::encode($model->name) ?></li>
    <li><label>Email</label>: <?= Html::encode($model->email) ?></li>
    <li><label>Category</label>: <?=  Html::encode($model->category) ?></li>

</ul>

This is the image with the problem:

wqnecbli

wqnecbli1#

1.在扩展ActiveRecord的模型中,绝不能显式定义与DB中的列名相同的属性。
1.您必须为最终用户设置的每个属性至少定义一个验证规则,否则系统将不允许设置该规则。

相关问题