David
David

Reputation: 1

Yii2 How can I display Activeform radioList data on screen?

I have not experience with Yii2. I would like to be able to show the result of a form on the screen. With the fields name and email I have no problem but I can not show the selection that I make of my radioList. I have tried many methods but none works. Can you help me please?

These are my files.

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:

Upvotes: 0

Views: 342

Answers (1)

Bizley
Bizley

Reputation: 18021

  1. In model extending ActiveRecord you must never explicitly define properties that are the same as names of columns in DB.
  2. You must define at least one validation rule for each attribute that is set by the end user, otherwise system will not allow to set it.

Upvotes: 1

Related Questions