Yii表格输入

时间:2014-01-25 14:55:59

标签: php yii tabular

我想将表单中的多条记录添加到表格中。

我试图了解并遵循本指南并失败

Yii tabular input

http://www.yiiframework.com/doc/guide/1.1/en/form.table

我的代码无法保存。

我的控制器代码中有一条评论 A ,此处添加了isset()功能

这部分代码不会运行。

if(isset($_POST['MultiPart2'][$i]))

但是当我删除了isset()函数时,我得到错误未定义的偏移量:0

这意味着此代码

$_POST['MultiPart2'][$i]

不工作。

我该怎么办?

这是我的帖子

的var_dump
$_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"
   }
}

这是我的控制器代码

 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,
    ));
}

这是我的观看代码

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

以下是生成的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>

2 个答案:

答案 0 :(得分:2)

将文本输入的生成更改为:

     <?php echo $form->textField($model,"[$i]name",array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
                //...
     <?php echo $form->textField($model,"[$i]age",array('size'=>60,'maxlength'=>100, "value"=>"5")); ?>
                //..
     <?php echo $form->textField($model,"[$i]sex",array('size'=>60,'maxlength'=>100, "value"=>"m")); ?>

它在double qoutes "中的换行输入名称(现在$i并不解释为varialble - 因为它在sinqle qoutes中)。

也可以尝试更改:

 $model=array();
for ($i=0; $i < 2; $i++) { 
    $model[$i]=new MultiPart2;
}

if(isset($_POST['MultiPart2']))
{   

    foreach ($model as $i=>$mod) {

        if(isset($_POST['MultiPart2'][$i]))
         // A
        //with isset() the following code does not run
        {       

            $mod->attributes=$_POST['MultiPart2'][$i];
            if($mod->save())
                echo "Saved";
        }
    }
}

EDIT_2 _ 将查看代码更改为(名称相同的错误):

    foreach ($model as $i => $mod) {
        ?>

            <div class="row">
                <?php echo $form->labelEx($mod,'name'); ?>
                <?php echo $form->activeTextField($mod,"[$i]name",array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
                <?php echo $form->error($mod,'name'); ?>
            </div>
            //... other inputs

或尝试:

        foreach ($model as $i => $mod) {
        ?>

            <div class="row">
                <?php echo $form->labelEx($mod,'name'); ?>
                <?php echo $form->textField("MultiPart2[$id]['name']",$mod['name'],array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
                <?php echo $form->error($mod,'name'); ?>
            </div>



            <div class="row">
                <?php echo $form->labelEx($mod,'age'); ?>
                <?php echo $form->textField("MultiPart2[$id]['age']",$mod['age'],array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
                <?php echo $form->error($mod,'age'); ?>
            </div>


            <div class="row">
                <?php echo $form->labelEx($mod,'sex'); ?>
                <?php echo $form->textField("MultiPart2[$id]['sex']",$mod['sex'],array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
                <?php echo $form->error($mod,'sex'); ?>
            </div>

答案 1 :(得分:0)

您应该更改您的视图代码,如下所示:

删除''中名称周围的"[$i]['name']" - 以及所有字段

<?php echo $form->textField($model,"[$i][name]",array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>

并且还改变了foreach中的变量名称,如Voodoo417所说。

foreach ($model as $i => $mod) {