Yii2 DynamicForm:演示3:具有房间类型的嵌套动态表单

时间:2017-03-28 14:24:43

标签: yii2 dynamicform

是否可以在" demo 3: Nested Dynamic Form"中添加不同类型的房间。并按其类型分隔房间?例如,如果我想创建与演示3节目相同的并为每个房间添加类型。我想要有A型房间,B型房间和C型房间。有可能吗? (类型预先加载到数据库中,用户不必插入新类型也不必选择一种类型。如果我要插入"房间类型A",我只需将其添加到它所属的位置)

我已经更改了html,但控制器中的房间阵列没有所有房间(因为第一个被覆盖)。那么,我需要做些什么来使其正常工作?

在图片中你可以看到这个方法,我想让它工作,因为只是编辑html没有工作。

approach

1 个答案:

答案 0 :(得分:0)

好的,这是我的解决方案:

再创建2个"模型"的扩展名。所以我有3个分支:房间(ActiveRecord),RoomB(模型),RoomC(模型)。所以我可以代表3种类型的房间。 两个"模型"的属性。 clases,是" id"以及"描述" " ActiveRecord"上课(记住,我们在谈论房间)。 在" _form.php"中,我放了两个"渲染(' _form-rooms')"更多,内部div与class" col-md-4"得到分离。

<td>
<div class="row">
<div class="col-md-4">
                    <?= $this->render('_form-rooms', [
                        'form' => $form,
                        'indexHouse' => $indexHouse,
                        'modelsRoom' => $modelsRoom[$indexHouse],
                    ]) ?>

<button type="button" class="remove-house btn btn-danger btn-xs"><span class="fa fa-minus"></span></button>
</div>
<div class="col-md-4">
                    <?= $this->render('_form-rooms', [
                        'form' => $form,
                        'indexHouse' => $indexHouse,
                        'modelsRoom' => $modelsRoomB[$indexHouse],
                    ]) ?>

<button type="button" class="remove-house btn btn-danger btn-xs"><span class="fa fa-minus"></span></button>
</div>
<div class="col-md-4">
                    <?= $this->render('_form-rooms', [
                        'form' => $form,
                        'indexHouse' => $indexHouse,
                        'modelsRoom' => $modelsRoomC[$indexHouse],
                    ]) ?>

<button type="button" class="remove-house btn btn-danger btn-xs"><span class="fa fa-minus"></span></button>
</div>
</div>
                </td>

在&#34; actionCreate&#34;中,我做了2个额外的数组,代表2&#34;模型&#34; clases,所以我有3个数组:$ modelsRoom = [[new Room]]; $ modelsRoomB = [[new RoomB]]; $ modelsRoomC = [[new RoomC]]; 我已经改变了#34; actionCreate&#34;内部代码的所有逻辑。 ad hoc与两个额外的数组,例如,在&#34; isset($ _ POST [&#39; Room&#39;] [0] [0])&#34;,我&#39; ve要求&#34; isset($ _ POST [&#39; RoomB&#39;] [0] [0])&#34;和#34; isset($ _ POST [&#39; RoomC&#39;] [0] [0])&#34;以及:

// validate person and houses models
           $valid = $modelPerson->validate();
           $valid = Model::validateMultiple($modelsHouse) && $valid;
           $valid2 = $valid3 = $valid;

           if (isset($_POST['Room'][0][0])) {
               foreach ($_POST['Room'] as $indexHouse => $rooms) {
                   foreach ($rooms as $indexRoom => $room) {
                       $data['Room'] = $room;
                       $modelRoom = new Room;
                       $modelRoom->load($data);
                       $modelsRoom[$indexHouse][$indexRoom] = $modelRoom;
                       $valid = $modelRoom->validate();
                   }
               }
           }

           if (isset($_POST['RoomB'][0][0])) {
               foreach ($_POST['RoomB'] as $indexHouse => $rooms) {
                   foreach ($rooms as $indexRoom => $room) {
                       $data['Room'] = $room;
                       $modelRoom = new Room;
                       $modelRoom->load($data);
                       $modelsRoomB[$indexHouse][$indexRoom] = $modelRoom;
                       $valid2 = $modelRoom->validate();
                   }
               }
           }


           if (isset($_POST['RoomC'][0][0])) {
               foreach ($_POST['RoomC'] as $indexHouse => $rooms) {
                   foreach ($rooms as $indexRoom => $room) {
                       $data['Room'] = $room;
                       $modelRoom = new Room;
                       $modelRoom->load($data);
                       $modelsRoomC[$indexHouse][$indexRoom] = $modelRoom;
                       $valid3 = $modelRoom->validate();
                   }
               }
           }

所以我问两个额外的&#34;有效&#34;变量是真的继续

if ($valid && $valid2 && $valid3) {
                $transaction = Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $modelPerson->save(false)) {
                        foreach ($modelsHouse as $indexHouse => $modelHouse) {

                            if ($flag === false) {
                                break;
                            }

                            ... (continue with the same code)

并且,在表单的渲染中,我作为参数传递了额外的数组:

return $this->render('create', [
            'modelPerson' => $modelPerson,
            'modelsHouse' => (empty($modelsHouse)) ? [new House] : $modelsHouse,
            'modelsRoom' => (empty($modelsRoom)) ? [[new Room]] : $modelsRoom,
            'modelsRoomB' => (empty($modelsRoomB)) ? [[new RoomB]] : $modelsRoomB,
            'modelsRoomC' => (empty($modelsRoomC)) ? [[new RoomC]] : $modelsRoomC,
        ]);

在&#34; _form.php&#34;查看,你可以看到上面的代码,我在两个额外的&#34; _form-rooms&#34;的渲染中使用了两个额外的数组。

在&#34; _form-rooms&#34;中,我删除了代表模特ID的代码,因为在&#34; actionUpdate&#34;中,我已经删除所有&#34; Houses&#34;,以及所有他们的&#34;房间&#34;也将被删除。在此之后,我就像在&#34; actionCreate&#34;中做同样的事情。 (在帖子之后)。

我希望你能理解我的解决方案。它可能不是最好的解决方案,但它对我有用。我还有更多详细信息,以免过多延伸此回复,但您可以随时与我联系;)

如果您需要更多详细信息,请给我发电子邮件。