S
S
Sergey Belyakov2020-02-19 09:06:41
Yii
Sergey Belyakov, 2020-02-19 09:06:41

How to correctly pass two objects of the same yii2 form?

Good afternoon! I cannot process two objects of the same form.
The meaning is this. I have a model, it doesn't pull anything from the base, it's just a form with custom fields, in my case just two dates. But I will use this form simultaneously on several reports. So I am creating two objects of the same form in the controller.
Here is the model:

<?php


namespace app\models;


use yii\base\Model;

class ReportsForm extends Model
{
    public $date_one;
    public $date_two;

    public function attributeLabels()
    {
        return [
            'date_one' => 'Дата начала',
            'date_two' => 'Дата окончания',
        ];
    }

    public function rules(){
        return [
            [['date_one','date_two'],'required'],
            [['date_one','date_two'],'safe'],
        ];
    }
}


Here is the controller:
public function actionEpicrisis(){
        $epicrisis = new ReportsForm(); // Форма для Отчета1
        $analyzes = new ReportsForm(); // Форма для Отчета2

        return $this->render('epicrisis',compact('epicrisis','analyzes'));
    } // Обработка отчетов

And here is the action
<h2 class="animated bounceInRight">ОТЧЕТЫ</h2>
    <h2>ВЫБЕРИТЕ ПАРАМЕТРЫ</h2>
    <hr>
    <?php $form = ActiveForm::begin([
            'id'=>'report1'
    ])?>
    <?= $form->field($epicrisis,'date_one')->widget(DatePicker::class)?>
    <?= $form->field($epicrisis,'date_two')->widget(DatePicker::class)?>
    <?= Html::submitButton('Применить',['class' => 'btn btn-primary'])?>
    <?php $form = ActiveForm::end()?>

    <hr>
    <?php $form1 = ActiveForm::begin([
        'id'=>'report2'
    ])?>
    <?= $form1->field($analyzes,'date_one')->widget(DatePicker::class)?>
    <?= $form1->field($analyzes,'date_two')->widget(DatePicker::class)?>
    <?= Html::submitButton('Применить',['class' => 'btn btn-primary'])?>
    <?php $form1 = ActiveForm::end()?>
    <hr>

Further, I pass these forms to the action, but the second form does not work (it goes below in the code). So the first one works and the second one doesn't. If I comment on the first form (This is all an action), then the second one works. I don't understand what the problem is, please help me.
I have a simple date widget on two forms, but on the second form it does not work, if you forcefully enter data there and click Apply, then the first form works, although the data was sent from the second

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
Z
zerg2000, 2020-02-19
@grey_18_08

Your problems from that name at the form Identical. Those. output - two forms.
In order not to produce the same, you can use inheritance. Those.
ReportsForm
and
ReportsForm2 extend ReportsForm
$epicrisis = new ReportsForm(); // Form for Report1
$analyzes = new ReportsForm2(); // Form for Report2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question