php - property_path Symfony 表单选项中方括号的含义是什么

标签 php forms symfony

我在一个项目中看到 property-path可以与方括号一起使用,如下所示:

foreach ($foo as $i => $object) {
    $builder->add("foo_{$i}", 'checkbox', array(
        'property_path' => "[$i].foo",
    ));
}

这似乎将每个字段与给定的 id 对象的 foo 属性相关联,但我还没有找到任何有关它的文档。仅this example 。 知道它是什么意思以及它记录在哪里吗?

最佳答案

这就是 Symfony UPGRADE from 2.0 to 2.1说:

The mapping of property paths to arrays has changed.

Previously, a property path "street" mapped to both a field $street of a class (or its accessors getStreet() and setStreet()) and an index ['street'] of an array or an object implementing \ArrayAccess.

Now, the property path "street" only maps to a class field (or accessors), while the property path "[street]" only maps to indices.

另请参阅 Symfony 的有关 the PropertyAccess component 的文档。 .

property_path的目的在 FormType Field 中进行了解释页面,但我想您已经知道它的用途了。

对于您发布的代码,我猜想(我只使用过 Symfony 表单一次,只是触及了表面) 对于每个属性 bar对象 $foo它创建了一个 <input type="checkbox" name="foo_bar"> .

提交表单后,property_path告诉表单输入名为 foo_bar 的复选框的值进入$data['bar']->foo哪里$data是方法 getData() 返回的对象的形式。

关于php - property_path Symfony 表单选项中方括号的含义是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729015/

相关文章:

php - html5 表单没有反应提交

css - 将颜色属性添加到 form_error

javascript - Creating a HTML form with selector 已保存的 SQL 十六进制颜色的选择器

php - 如何在不删除PHP的情况下将新数据添加到.txt的顶部

php - 如何在PHP中获取 sphinx 连接失败错误

php - 在其他 Blade 中包含导航栏 Blade 会使 "CSS"与 "Laravel"发生冲突!

javascript - Jquery form serialize() 只返回部分字段

php - Symfony FOSUserBundle 翻译不显示

php - FOSElasticaBundle CustomScore查询分页

javascript - 谁用 Jquery Ajax 附加 php onchange