I want to add multiple records from a form into a table.
I have tried to understand and follow this guide and failed
http://www.yiiframework.com/doc/guide/1.1/en/form.table
My code does not save.
I have a comment in my controller code written A, here with the isset()
function added
this part of the code will not run.
if(isset($_POST['MultiPart2'][$i]))
But when I removed the isset() function I get the error Undefined offset: 0
This means this code
$_POST['MultiPart2'][$i]
Is not working.
What should I do?
Here is the var_dump of my post
$_POST['MultiPart2']
array(2) {
["[0"]=>
array(3) {
["'name'"]=>
string(3) "Vic"
["'age'"]=>
string(2) "25"
["'sex'"]=>
string(1) "m"
}
["[1"]=>
array(3) {
["'name'"]=>
string(3) "Vic"
["'age'"]=>
string(2) "25"
["'sex'"]=>
string(1) "m"
}
}
Here is my controller code
public function actionCreate()
{
//insert multiple instances of model into array
$model=array();
for ($i=0; $i < 2; $i++) {
$model[]=new MultiPart2;
}
if(isset($_POST['MultiPart2']))
{
foreach ($model as $i => $model) {
if(isset($_POST['MultiPart2'][$i]))
// A
//with isset() the following code does not run
{
$model->attributes=$_POST['MultiPart2'][$i];
if($model->save())
echo "Saved";
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
And here is my view code
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'multi-part2-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<?php
foreach ($model as $i => $model) {
?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,"[$i][name]",array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'age'); ?>
<?php echo $form->textField($model,"[$i][age]",array('size'=>60,'maxlength'=>100, "value"=>"5")); ?>
<?php echo $form->error($model,'age'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'sex'); ?>
<?php echo $form->textField($model,"[$i][sex]",array('size'=>60,'maxlength'=>100, "value"=>"m")); ?>
<?php echo $form->error($model,'sex'); ?>
</div>
<hr>
<?php
}
?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
Here is the generated HTML
<form id="multi-part2-form" action="/sam.com/system/index.php?r=multiPart2/create" method="post">
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<label for="MultiPart2_name" class="required">Name <span class="required">*</span> </label>
<input size="60" maxlength="100" value="Vic" name="MultiPart2[[0][name]]" id="MultiPart2__0_name" type="text" />
</div>
<div class="row">
<label for="MultiPart2_age" class="required">Age <span class="required">*</span></label>
<input size="60" maxlength="100" value="5" name="MultiPart2[[0][age]]" id="MultiPart2__0_age" type="text" />
</div>
<div class="row">
<label for="MultiPart2_sex" class="required">Sex <span class="required">*</span></label>
<input size="60" maxlength="100" value="m" name="MultiPart2[[0][sex]]" id="MultiPart2__0_sex" type="text" />
</div>
<hr>
<div class="row">
<label for="MultiPart2_name" class="required">Name <span class="required">*</span></label>
<input size="60" maxlength="100" value="Vic" name="MultiPart2[[1][name]]" id="MultiPart2__1_name" type="text" />
</div>
<div class="row">
<label for="MultiPart2_age" class="required">Age <span class="required">*</span></label>
<input size="60" maxlength="100" value="5" name="MultiPart2[[1][age]]" id="MultiPart2__1_age" type="text" />
</div>
<div class="row">
<label for="MultiPart2_sex" class="required">Sex <span class="required">*</span></label>
<input size="60" maxlength="100" value="m" name="MultiPart2[[1][sex]]" id="MultiPart2__1_sex" type="text" />
</div>
<hr>
<div class="row buttons">
<input type="submit" name="yt0" value="Create" /> </div>
</form>