2

我有一个方法,验证某些输入的组合是有效的(例如选择了老年折扣选项并且生日显示年龄在 n 以上)。

/**
 * @Assert\True(message="you are too young for this option")
 */
public function isElderlyOptionValid()
{
    return 
    ($this->getElderlyOption() && $this->getAgeFromBirthday() <= 60);
}

现在我希望显示老年折扣选项的错误,而不是实体/表单的一般错误。所以我需要类似的东西:

/**
 * @Assert\True(message="you are too young for this option", bindTo="elderlyOption")
 */
public function isElderlyOptionValid()
{
    return 
    ($this->getElderlyOption() && $this->getAgeFromBirthday() <= 60);
}

我确实读过一次,这是可能的,但无法再次找到该文章。

4

1 回答 1

1

UniqueEntity 有 error_path 并atPath在 UniqueEntityValidator 内使用,但我认为不可能更改所有验证器的路径。

也许你可以扩展这个验证器并添加atPath功能,但是

也许这就是你要找的。表单具有error_mapping属性,您可以将错误“重定向”到字段。

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'error_mapping' => array(
            'matchingCityAndZipCode' => 'city',
        ),
    ));
}
于 2015-12-30T05:23:09.573 回答