A
A
alysenko2014-10-22 01:14:30
Yii
alysenko, 2014-10-22 01:14:30

How to create a form with radio buttons in yii2?

It is necessary to implement something similar to an interactive test in yii2. According to the found example, I created the following:
in views:

<?php $form = ActiveForm::begin(); ?>
    <?= $form-> field ($modeltest, 'yes')->radio () ?>
    <?= $form-> field ($modeltest, 'no')->radio () ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

in models :
class TestForm extends Model
{
    public $yes;
    public $no;
    
    public function rules () {
        
        return [
            [['yes', 'no'], 'required'],
        ];    
    }
}

in controllers just created a new instance and passed it to render
$modeltest = new TestForm;

return $this->render('main-confirm2', ['model' => $model, 
                                                   'test' => $testsql->sql('test01', 1),
                                                   'modeltest' => $modeltest,
                                                  ]);

plus made loading of the test from a DB.
But I just can’t figure out how to pass my values ​​​​from the database instead of yes and no to views . That is, not just the words yes and no, but to pass variables.
Can you please tell me how this can be implemented? If possible with an example.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
bzz, 2014-10-24
@bzz

<?= $form->field($model, 'Name')
                ->radioList(ArrayHelper::map(TestForm::find()->all(), 'id', 'name')) ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question