I
I
IvanMogilev2020-05-02 19:38:42
Yii
IvanMogilev, 2020-05-02 19:38:42

How to make a model?

Hello, I'm just starting to learn yii, I want to make a site with tests, a controller model and a view for the form, I'm attaching the
Model below

<?php
namespace app\models;

use yii\base\Model;

class QuizForm extends Model
{
    public $radio1;
    public function rules()
    {
        return [
            [['radio1'], 'required'],
        ];
    }
}

Controller
<?php


namespace app\controllers;

use Yii;
use app\models\QuizForm;
use yii\web\Controller;


class QuizController extends Controller
{
    public function actionIndex()
    {
        $model = new QuizForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate())
        {
            return $this->render('res', ['model' => $model]);
        }
        else
        {
            return $this->render('index', ['model' => $model]);
        }

    }
}


index.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
                <div class="tests-content">
                    <div class="test-title">
                        Тест 1. HTML5 теги
                    </div>
                    <?php $form = ActiveForm::begin(); ?>
                    <div class="test-variants">
                            <?= $form->field($model, 'radio1')->radioList( [1=>'Для вставки изображения', 2 => 'Для отправки данных', 3 => 'Для вставки пробела'] )->label(' Для чего предназначен тег img ?'); ?>
                    </div>
                    <div class="test-buttons">
                        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
                    </div>
                    <?php ActiveForm::end(); ?>

                </div>

The essence of the question is that in the model for the form, the number of $radio1 type variables should be equal to the number of questions that I will receive from the database. Please let me know how to do this, thanks in advance.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Arthur, 2020-05-02
@IvanMogilev

make a question model and write a one-to-many relationship in the test model, there are examples in the ui
manual

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question