【问题标题】:Codeigniter: set_value not re-populating correctly array form fieldCodeigniter:set_value 未正确重新填充数组表单字段
【发布时间】:2012-12-20 05:36:05
【问题描述】:

我有一个带有 CI 验证的表单,它使用数组作为输入名称。视图最初有这样的代码:

<input type="text" name="feed_urls[]"
   value="<?php echo set_value('feed_urls[]', ''); ?>" >

当加载到浏览器中时会正确转换为:

<input type="text" name="feed_urls[]" value="">

然后通过 Jquery,用户可以在提交之前向 DOM 添加更多相同的&lt;input&gt;,因此最终发布的内容可能是例如

<input type="text" name="feed_urls[]" value="">
<input type="text" name="feed_urls[]" value="">
<input type="text" name="feed_urls[]" value="">

现在,如果提交的数据通过验证,则一切正常并存储在 DB 中。但是如果验证失败,控制器会发送回视图,但我看不到 POSTed 表单的 N &lt;input&gt;。我只看到一个并且它是空的,这是可以理解的,因为我没有向 set_values() 提供第二个参数,但是再次预期的是通过 $this->input->post 重新填充输入(feed_urls) 数组,其中包含无效的 POST 数据。

我确实在控制器上验证 $this->input->post('feed_urls') 是否有发布的内容(无效或无效)。

我已阅读关于使用validation with arrays as field names 的 CI 用户指南文档

关于 set_value() 的正确用法有什么想法吗?正确是指在验证失败时,我得到了 N 个已发布的输入,并一一正确地重新填充。

【问题讨论】:

    标签: forms codeigniter validation populate


    【解决方案1】:

    你可以使用:

    <input type="text" name="feed_urls[]" value="<?php echo set_value('feed_urls[0]'); ?>">
    

    我已经在 CI 2.0 上对其进行了测试,但它应该也可以在以前的版本中使用。

    干杯!

    【讨论】:

    • 但是如何知道应该输出多少这样的输入呢?无论如何至少要获得数组的大小?
    【解决方案2】:

    由于您只提供了一小部分代码,我只能就我认为您的主要问题给您一个提示。

    可以使用jQuery填充输入数组的第n个元素,如下所示:

    $("[id^=feed_urls]").eq(0).val(someURL);
    $("[id^=feed_urls]").eq(1).val(someURL);
    $("[id^=feed_urls]").eq(2).val(someURL);
    

    等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多