Answer the question
In order to leave comments, you need to log in
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'],
];
}
}
public function actionEpicrisis(){
$epicrisis = new ReportsForm(); // Форма для Отчета1
$analyzes = new ReportsForm(); // Форма для Отчета2
return $this->render('epicrisis',compact('epicrisis','analyzes'));
} // Обработка отчетов
<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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question