大家好!
问题
我无法找到将getter验证错误附加到表单中特定字段的解决方案。
我的实体中有一个方法Presale::hasPresaleProductsAdded()。此方法返回有关添加到集合的产品数量的真假。
提交表单后,验证错误会冒泡到父窗体(因为表单上没有"presaleProductsAdded“字段)。我想将此错误附加到"presaleProducts“字段。
我知道error_mapping属性,但我不能让它工作
代码
这是我的validation.yml
OQ\PresaleBundle\Entity\Presale:
properties:
name:
- NotBlank: ~
description:
- NotBlank: ~
company:
- NotBlank: ~
getters:
presaleProductsAdded:
- "True": { message: "Specify at least one product" }可能的解决方案
我知道这个问题可以通过自定义验证约束类来解决。但是问题是--我只能用validation.yml、实体方法和getter约束来实现它吗?
发布于 2015-11-25 04:51:00
所以,我知道了。
1)我忘记了error_bubbling选项。Presale::presaleProducts属性具有在表单中分配的自定义字段类型。自定义字段类型是复合字段,父类型设置为"form“。在这种情况下,默认情况下error_bubbling是true。
变成假的:
class PresaleProductsType extends AbstractType
{
...
/**
* @param OptionsResolverInterface $resolver
*/
public
function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
[
'data_class' => 'OQ\PresaleBundle\Entity\PresaleProducts',
'error_bubbling' => false, // That's it!
]
);
}
/**
* @return string
*/
public
function getName()
{
return 'oq_presale_products';
}
/**
* @return string
*/
public
function getParent()
{
return 'form';
}
...
}2) error_mapping选项在PresaleType表单中配置如下:'hasPresaleProductsAdded' => 'presaleProducts'
错误在于属性路径名称: symfony没有找到public $hasPresaleProductsAdded;,并试图找到公共getter (或isser或hasser),例如:
Presale::getHasPresaleProductsAdded()Presale::hasHasPresaleProductsAdded()Presale::isHasPresaleProductsAdded()但是实体类定义中只有Presale::hasPresaleProductsAdded()。
因此,我修正了error_mapping选项:
'error_mapping' => array(
'presaleProductsAdded' => 'presaleProducts',
),一切都开始像魅力一样发挥作用!
https://stackoverflow.com/questions/33890848
复制相似问题