Search code examples
phpyii2yii2-basic-appyii2-modelactive-form

Yii2. Multiple model items in one form


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

Solution

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