Answer the question
In order to leave comments, you need to log in
How to process a form (multiple forms on one page) with unknown fields?
Good afternoon.
The bottom line is that there is a page on which the user somehow creates forms and places them on one page. Accordingly, the user can give any name for the field.
How can you handle such a page with multiple forms with unknown field names?
For example, when we process one form with known fields, it looks like this:
Form model, where the necessary fields and validation rules are set:
<?php
namespace app\models;
use yii\base\Model;
class EntryForm extends Model
{
public $name;
public $email;
public function rules()
{
return [
[['name', 'email'], 'required'],
['email', 'email'],
];
}
}
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\EntryForm;
class SiteController extends Controller
{
// ...существующий код...
public function actionEntry()
{
$model = new EntryForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// данные в $model удачно проверены
// делаем что-то полезное с $model ...
return $this->render('entry-confirm', ['model' => $model]);
} else {
// либо страница отображается первый раз, либо есть ошибка в данных
return $this->render('entry', ['model' => $model]);
}
}
}
Answer the question
In order to leave comments, you need to log in
Something tells me that I need to do an iteration, for example, foreach (if I understand the essence of the question correctly)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question