M
M
Maxim2016-05-13 15:44:16
Yii
Maxim, 2016-05-13 15:44:16

How to add variables to form model?

There is a database, there are two tables in it:
1. Table1
2. Table2
In Table2 there are lines: id and Name
The site has two pages:
1. Main page
2. Add
page On the Add page there is a form in which there is an input, using of this form, I add records to table2.
This page is ok.
Task with the main page.
There is a form here, what is its essence:
I get the first record from the table2 table, for example there will be Name1
In the form I create a checkbox with the name Name1 and so on in a circle, as long as there are records in the table2 table.
Here is the form code in Yii2

<?php $form = ActiveForm::begin(); ?>

    <?php foreach($allNameEx as $nameEx) : ?>

        <?= $form->field($model, $nameEx['name'])->checkbox() ?>

    <?php endforeach; ?>
 
    <div class="form-group">
        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
    </div>

$model is $model = new form();
$allNameEx is an array that gets all Name records from table2
This code would work, but when creating a form on the page, there is a form model file in which you need to add variables the same as in the form input.
If <?= $form->field($model, $nameEx['name'])->checkbox() ?> it would be, for example
<?= $form->field($model, 'Name1')-> checkbox() ?>, then the form model must contain the public $Name1 variable accordingly.
So the question is how to add these variables to the form model?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2016-05-13
@webinar

If you have not 1 checkbox, but many, then you should probably use checkboxlist and pass an array to it
in the model:

public function getMyList(){
$arr = [];
return $arr;
}

D
Decadal, 2016-05-13
@Decadal

The model describes the structure of an entity as such. If you retrieve all records from the database, then you form an array of data describing the same entity. Therefore, to process the array, you need to create another model that will contain the Names array, and now your checkboxes will fall into it. By the way, it would be easier to help you if you said what the main form does.
In the view, in order to pass an array of data, you need to render something like <input name="names[]" type="checkbox">
in a loop
how to do it in the yii2 style, I don’t know, I didn’t wonder (by the way, yii in tags)
upd:
once again: these variables do not need to be described in the model by name, since they have the same meaning - these are the values ​​of the "name" field.
Therefore, they should be stored in another model, which has a names field denoting an array of names. Then your question will go away from creating a dynamic model with fields based on data, and will only rest on rendering checkboxes from the data array and passing the array to the model field.

M
Maxim, 2016-05-13
@KidsBout

It worked,
<?= $form->field($model, 'nameEx[]')->checkboxList(form::checkFormData()); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question