dongpu2476 2012-06-12 21:00
浏览 46
已采纳

Yii框架:表单验证失败后的空变量

I have a form with two input text fields:

<input id="ModelName_test_0" name="ModelName[test][0]" type="text">
<input id="ModelName_test_1" name="ModelName[test][1]" type="text">

These input fields get generated with the following commands:

<?php echo $form->textField($model,'test[0]'); ?>
<?php echo $form->textField($model,'test[1]'); ?>

Now, when I submit the form I see the values in the POST request. However, when the form submit fails then I can not get the values back into the input fields. Printing the model it shows that there are no values for $test; - is this because $test is an array in the form?

Even after the validation all values are still assigned to the variables:

if($model->validate()) {
   echo "<pre>";
   print_r($_POST);
   return;
}

This returns:

[ModelName] => Array  
        (
            [test] => Array  
                (
                    [0] => myFirstInputField  
                    [1] => mySecondInputField
                )
        )

So the values are in the POST but after the failed validation they are gone and I get empty variables:

[ModelName] => Array  
        (
            [test] => 
        )

The variable test is declared safe in the validation rules.

What I want to achieve is:
If the validation fails put the entered values back into the appropriate input text fields.

Any pointers in the right direction would be helpful :)

  • 写回答

2条回答 默认 最新

  • dtq26360 2012-06-13 14:03
    关注

    I found this article on the yiiframework.com website which helped me solving this issue: http://www.yiiframework.com/doc/guide/1.1/en/form.table

    This is the sample code which you would put it your controller:

    public function actionBatchUpdate()
    {
        // retrieve items to be updated in a batch mode
        // assuming each item is of model class 'Item'
        $items=$this->getItemsToUpdate();
        if(isset($_POST['Item']))
        {
            $valid=true;
            foreach($items as $i=>$item)
            {
                if(isset($_POST['Item'][$i]))
                    $item->attributes=$_POST['Item'][$i];
                $valid=$item->validate() && $valid;
            }
            if($valid)  // all items are valid
                // ...do something here
        }
        // displays the view to collect tabular input
        $this->render('batchUpdate',array('items'=>$items));
    }
    

    And this is what the view would look like:

    <div class="form">
    <?php echo CHtml::beginForm(); ?>
    <table>
    <tr><th>Name</th><th>Price</th><th>Count</th><th>Description</th></tr>
    <?php foreach($items as $i=>$item): ?>
    <tr>
    <td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
    <td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
    <td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
    <td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
    </tr>
    <?php endforeach; ?>
    </table>
    
    <?php echo CHtml::submitButton('Save'); ?>
    <?php echo CHtml::endForm(); ?>
    </div><!-- form -->
    

    Both code snippets are taken from yiiframework.com where you can find more details on how to use 'Tabular Inputs': http://www.yiiframework.com/doc/guide/1.1/en/form.table

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分