【问题标题】:Yii Cactiveform get urlYii Cactiveform 获取 url
【发布时间】:2013-08-08 15:42:18
【问题描述】:

我在 Yii 中使用 get 方法与 Cactiveform 作斗争。这是我的代码:

<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id'=>'inlineForm',
    'type'=>'inline',
  'method'=>'get',
  'action'=> array('searchview'),
    'htmlOptions'=>array('class'=>'well'),
)); 
echo $form->dropDownListRow($model, 'year',$arrYears); 
echo $form->dropDownListRow($model, 'month',$mons); 
echo $form->dropDownListRow($model, 'day',$days); 
$this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary','label'=>'Vyhledat')); 
  $this->endWidget(); ?>

当我点击提交按钮时,我得到了这个 URL:

http://stavbadomusvepomoci.cz/stavba_naklady/index.php?r=expenses%2Fsearchview&Expenses%5Byear%5D=2013&Expenses%5Bmonth%5D=0&Expenses%5Bday%5D=0&yt0=

问题是字段的名称是 Expenses[year] 插入的只是:year。当我删除它时,这个网址就可以正常工作了。

http://stavbadomusvepomoci.cz/stavba_naklady/index.php?r=expenses%2Fsearchview&year=2013&month=0&day=0&yt0=

我的搜索返回 400 错误,因为它无法识别该 url。我使用调节器 CHtml::beginForm 进行这项工作,但我希望这可以使用 CActive 表单。

所以我的问题是:我怎样才能摆脱 url 中的 Expenses[] 以便 cactiveform 起作用。

感谢您的回答!

【问题讨论】:

    标签: yii


    【解决方案1】:

    我建议在服务器端使用 $_GET["Expenses"] 而不是整个 $_GET 数组,因为它可能包含与搜索无关的变量,例如 CSRF 令牌。

    我看到实现您想要的唯一方法是 dropDownListRow 的 htmlOptions 参数。你可以给它一个特定的名称,这会导致 yii 不设置模型属性依赖名称。

    $form->dropDownListRow($model, 'year', $arrYears, array("name" => "year")); 
    

    【讨论】:

    • 太棒了!添加特定名称就可以了。没想到我能做到!我每天都在跟 Yii 学习新东西。非常感谢!
    • @Vojtech 没问题,你通常可以给 htmlOptions 数组(它出现在 Yii 的各个地方,你可以看到here)任何应该出现在标签本身的键值对。通常你不应该过度使用,尤其是考虑创建你自己的 CHtml 子类来定义你自己的元素类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 2015-12-20
    • 2012-06-07
    相关资源
    最近更新 更多