I want validate announcement, prices and contact informations, but I do not have clue how to handle multiple models in controller. In yii1 I would do something like this:
if (!empty($_POST['Price'])) {
foreach($_POST['Price'] as $priceData) {
$model = new Price();
$model->setAttributes($priceData);
$prices[] = $model;
}
}
if (!empty($_POST['Contact'])) {
foreach($_POST['Contact'] as $contactData) {
$model = new Contact();
$model->setAttributes($contactData);
$contacts[] = $model;
}
}
But how to do it in Yii2 and additionaly validate multiple models?
I followed this Collecting tabular input, because I have arrays of different kinds of models. At the end there is "In the view you can use javascript to add new input lines dynamically.". I know how to make ajax loading, but what exactly view file should contain? I tried:
<?php
use yii\widgets\ActiveForm;
use common\models\Price;
$price = new Price();
echo ActiveForm::field($price, "amount");
echo ActiveForm::field($price, "description");
but it result in error "Getting unknown property: yii\web\View::fieldConfig".
Edit:
Now I have this in partal view:
<?php
use yii\helpers\Html;
use common\models\Price;
$price = new Price();
echo Html::activeTextInput($price, "[$index]amount");
echo Html::error($price, "[$index]description");