Yii2。一种形式的多个模型项目

Yii2. Multiple model items in one form

我有这样的看法ActiveForm。如何允许用户在此视图中有 select 个子项(表单项)并提交所有 个子项 相同的模型项。

<?php $form = ActiveForm::begin([]) ?>
    <div class="child_card">

            <?=$form->field($child, 'first_name', ['inputOptions' => ['placeholder' => 'Enter Your First Name']])?>

                <?=$form->field($child, 'middle_name',
                    ['inputOptions' => ['placeholder' => 'Enter Your Middle Name']])?>

                <?=$form->field($child, 'last_name', ['inputOptions' => ['placeholder' => 'Enter Your Last Name']])?>
                <br>

                <?=$form->field($child, 'gender', [
                    'template'     => '{label} <div class="field">{input}{error}{hint}</div>',
                    'inputOptions' => [
                        'placeholder' => 'Gender',
                    ],
                ])->dropDownList([
                    'male'   => 'Male',
                    'female' => 'Female',
                ], ['class' => 'ui dropdown selection', 'prompt' => 'Gender'])->label('Select your gender')?>

                <?=$form->field($child, 'birthdate_month', [
                    'template'     => '{label} <div class="field">{input}{error}{hint}</div>',
                    'inputOptions' => [
                        'placeholder' => 'Month',
                    ],
                ])->dropDownList([
                    'January'   => 'January',
                    'February'  => 'February',
                    'March'     => 'March',
                    'April'     => 'April',
                    'May'       => 'May',
                    'June'      => 'June',
                    'July'      => 'July',
                    'August'    => 'August',
                    'September' => 'September',
                    'November'  => 'November',
                    'December'  => 'December',
                ], ['class' => 'ui dropdown selection', 'prompt' => 'Month'])->label('Birthdate')?>


                <?=$form->field($child, 'birthdate_day', [
                    'template'     => '{label} <div class="field">{input}{error}{hint}</div>',
                    'inputOptions' => [
                        'placeholder' => 'Day',
                    ],
                ])->dropDownList([
                    '1'  => '1',
                    '2'  => '2',
                    '3'  => '3',
                    '4'  => '4',
                    '5'  => '5',
                    '6'  => '6',
                    '7'  => '7',
                    '8'  => '8',
                    '9'  => '9',
                    '10' => '10',
                    '11' => '11',
                    '12' => '12',
                    '13' => '13',
                    '14' => '14',
                    '15' => '15',
                    '16' => '16',
                    '17' => '17',
                    '18' => '18',
                    '19' => '19',
                    '20' => '20',
                    '21' => '21',
                    '22' => '22',
                    '23' => '23',
                    '24' => '24',
                    '25' => '25',
                    '26' => '26',
                    '27' => '27',
                    '28' => '28',
                    '29' => '29',
                    '30' => '30',
                    '31' => '31',
                ], ['class' => 'ui dropdown selection', 'prompt' => 'Day'])->label(false)?>

                <?=$form->field($child, 'birthdate_year', [
                    'template'     => '{label} <div class="field">{input}{error}{hint}</div>',
                    'inputOptions' => [
                        'placeholder' => 'Year',
                    ],
                ])->dropDownList([
                    '2016' => '2016',
                    '2015' => '2015',
                    '2014' => '2014',
                    '2013' => '2013',
                    '2012' => '2012',
                    '2011' => '2011',
                    '2010' => '2010',
                    '2009' => '2009',
                    '2008' => '2008',
                    '2007' => '2007',
                    '2006' => '2006',
                    '2005' => '2005',
                    '2004' => '2004',
                    '2003' => '2003',
                    '2002' => '2002',
                    '2001' => '2001',
                    '2000' => '2000',
                    '1999' => '1999',
                    '1998' => '1998',
                    '1997' => '1997',
                    '1996' => '1996',
                    '1995' => '1995',
                ], ['class' => 'ui dropdown selection', 'prompt' => 'Year'])->label(false)?>

                <?=$form->field($child, 'country_of_birth',
                    ['inputOptions' => ['placeholder' => "Enter Your Child's Country of Birth"]])
                        ->label('Your Child’s Country of Birth:')?>

                <?=$form->field($child, 'city_of_birth',
                    ['inputOptions' => ['placeholder' => "Enter Your Child's City of Birth"]])
                        ->label('Your Child’s City/Town of Birth:')?>
        </div>
        <?php ActiveForm::end(); ?>

只需添加模型 class 作为 public 字段并作为安全属性放入规则中

答案是使用一个简单的 for 循环。

视图示例

<?php $form = ActiveForm::begin() ?>

<?php foreach ($children as $i => $child): ?> //there are multiple persons (children)

...

 <?=$form->field($child, "[$i]birthdate_month", [...])->dropDownList([
                    '1'  => 'January',
                    ...
                ])?>

...

<?php endforeach; ?>

...

<?=Html::submitButton('Save', ['class' => 'ui primary button big'])?>
...

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

并在控制器中使用

Person::loadMultiple($children, Yii::$app->request->post());