Answer the question
In order to leave comments, you need to log in
How to validate a form with dynamically loaded radioLists?
How to validate dynamically loaded radiolists? Those. not a certain number, which depends on the number of records in the database.
It seems to have done something, but the validation does not pass + radioLists work as if it is 1 big ... that is, the checked item runs from one radioList to another
models/TestForm.php
<?php
namespace frontend\models;
use yii\base\Model;
class TestForm extends Model
{
public $radioButtonList = [];
public function rules()
{
return [
['radioButtonList', 'each', 'rule' => ['required']],
];
}
}
public function actionView($test_id)
{
...
$testForm = new TestForm();
if ($testForm->load(Yii::$app->request->post())) {
if ($testForm->validate()) {
var_dump($testForm);
}
else {
return 0;
}
}
...
return $this->render('view', [
...
'testForm' => $testForm,
...
]);
}
<?php
/* @var $this yii\web\View */
/* @var $model common\models\Test */
/* @var $dataProvider yii\data\ActiveDataProvider */
use common\models\Answer;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="test-view">
<?php $form = ActiveForm::begin(); ?>
<? for ($i = 0; $i < count($testQuestions); $i++) : ?>
<section class="question-<? echo $i+1 ?>">
<h3><? echo $testQuestions[$i]->question; ?></h3>
<?=
$form->field($testForm, 'radioButtonList')
->radioList(ArrayHelper::map(
Answer::find()->where(['question_id' => $testQuestions[$i]->id])->all(),
'id',
'value'
));
?>
</section>
<? endfor; ?>
<div class="form-group">
<?= Html::submitButton('Закончить тест', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Answer the question
In order to leave comments, you need to log in
To work with dynamic forms, it is better to use Dynamic Model , it eliminates a lot of problems and questions
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question