如何在Yii2上创建相同表单的动态列表?

时间:2017-06-14 01:57:18

标签: php yii2

如何创建相同表单的动态列表并在Yii2的视图上显示(可以将其视为具有不同信息的同一对象的列表)谢谢。

我在/frontend/views/site/example.php上有以下表格 那种形式我想把它放在一个清单上。

<div class="site-example">
    <h1><?= Html::encode($this->title) ?></h1>
    <p>Example of a list:</p>

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'form-example']); ?>

              <?=  $form->field($model, 'email')->textInput(['readonly' => true, 'value' => $email]) ?>

              <?=  $form->field($model, 'lastname')->textInput(['readonly' => true, 'value' => $lastname]) ?>

              <?=  $form->field($model, 'phone')->textInput(['readonly' => true, 'value' => $phone]) ?>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

</div>

1 个答案:

答案 0 :(得分:0)

假设这是你的controller

<?php

namespace frontend\controllers;

use Yii;
use yii\base\Model;
use yii\web\Controller;
use frontend\models\YourForm;

class SiteController extends Controller
{
    public function actionYourAction()
    {
        $forms = [new YourForm, new YourForm, new YourForm];

        return $this->render('example', [
            'forms' => $forms
        ]);
    }
}

然后您的view可能低于

<div class="site-example">
    <h1><?= Html::encode($this->title) ?></h1>
    <p>Example of a list:</p>

    <div class="row">
        <div class="col-lg-5">
            <?php foreach ($forms as $index => $form): ?>
                <?php $form = ActiveForm::begin(['options' => ['id' => "form-example-$index"]]); ?>

                  <?= $form->field($form, "[$index]email")->textInput(['readonly' => true, 'value' => $form->email]) ?>

                  <?= $form->field($form, "[$index]lastname")->textInput(['readonly' => true, 'value' => $form->lastname]) ?>

                  <?= $form->field($form, "[$index]phone")->textInput(['readonly' => true, 'value' => $form->phone]) ?>

                <?php ActiveForm::end(); ?>
            <?php endforeach; ?>
        </div>
    </div>
</div>