douzhang8840 2016-11-27 18:04
浏览 199
已采纳

Yii2。 一个表单中的多个模型项

I have such ActiveForm in view. How to allow user in this view select number of children (form items) and submit all number of children the same model items.

<?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(); ?>
  • 写回答

2条回答 默认 最新

  • duanhao4909 2016-12-07 06:59
    关注

    The answer is to use a simple for-loop.

    Example of the view

    <?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(); ?>
    

    And use in in controller

    Person::loadMultiple($children, Yii::$app->request->post());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)