R
R
Rafail Dunyashev2016-10-04 11:00:14
Yii
Rafail Dunyashev, 2016-10-04 11:00:14

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'],
        ];
    }
}

And processing in the controller:
<?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]);
        }
    }
}

So, what if these fields are unknown and there may be a different number of them?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2016-10-04
@NoizeMC

use DynamicModel for this purpose

P
Pavel Novikov, 2016-10-04
@paulfcdd

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 question

Ask a Question

731 491 924 answers to any question