2016-11-27 146 views
0

我有这样的ActiveForm。如何在此视图中允许用户选择一些儿童(表单项)并提交全部儿童人数相同型号的项目。Yii2。一种形式的多个模型项目

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

回答

0

答案是使用一个简单的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()); 
0

模型类只需添加在公共领域投入的规则,安全属性

+0

我的意思是多个.child_card –

+0

https://github.com/wbraganca/yii2-dynamicform –