首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Symfony2 validation propertyPath valus在方括号中?

为什么Symfony2 validation propertyPath valus在方括号中?
EN

Stack Overflow用户
提问于 2011-12-09 04:36:18
回答 2查看 4.2K关注 0票数 5

我正在验证一些值:

代码语言:javascript
复制
$collectionConstraint = new Collection(array(
    'email' => array(
        new NotBlank(),
        new Email(),
    ),
    'password'  => array(
         new NotBlank(),
         new MinLength(array('limit' => 6)),
         new MaxLength(array('limit' => 25)),
    ),
));
$data = array('email' => $this->getRequest()->get('email'), 'password' => $this->getRequest()->get('password'));
$errors = $this->get('validator')->validateValue($data, $collectionConstraint);

但是出于某种原因,字段(propertyPath)是用方括号存储的--我想知道为什么Sf会这样做。我不得不手动删除所有的括号,这看起来很荒谬,所以我认为我在某些地方缺少一些功能。

转储$errors:

代码语言:javascript
复制
Symfony\Component\Validator\ConstraintViolationList Object
(
    [violations:protected] => Array
        (
            [0] => Symfony\Component\Validator\ConstraintViolation Object
                (
                    [messageTemplate:protected] => This value should not be blank
                    [messageParameters:protected] => Array
                        (
                        )

                    [root:protected] => Array
                        (
                            [email] => 
                            [password] => 
                        )

                    [propertyPath:protected] => [email]
                    [invalidValue:protected] => 
                )

            [1] => Symfony\Component\Validator\ConstraintViolation Object
                (
                    [messageTemplate:protected] => This value should not be blank
                    [messageParameters:protected] => Array
                        (
                        )

                    [root:protected] => Array
                        (
                            [email] => 
                            [password] => 
                        )

                    [propertyPath:protected] => [password]
                    [invalidValue:protected] => 
                )

        )

)

即使是toString函数也是无用的。

代码语言:javascript
复制
"[email]: This value should not be blank","[password]: This value should not be blank"
EN

回答 2

Stack Overflow用户

发布于 2012-05-30 03:00:36

属性路径可以映射到属性,也可以映射到索引。考虑一个实现\ArrayAccess的类OptionBag和一个方法getSize()

属性路径[size]引用$optionBag->getSize()

  • The属性路径size引用
  • [size]引用$optionBag['size']

在本例中,您将验证一个数组。由于还可以通过索引访问数组元素,因此冲突中产生的属性路径包含方括号。

更新:

您不必手动删除方括号。您可以使用Symfony的PropertyAccess组件将错误映射到与您的数据具有相同结构的数组,例如:

代码语言:javascript
复制
$collectionConstraint = new Collection(array(
    'email' => array(
        new NotBlank(),
        new Email(),
    ),
    'password'  => array(
         new NotBlank(),
         new MinLength(array('limit' => 6)),
         new MaxLength(array('limit' => 25)),
    ),
));

$data = array(
    'email' => $this->getRequest()->get('email'), 
    'password' => $this->getRequest()->get('password')
);

$violations = $this->get('validator')->validateValue($data, $collectionConstraint);

$errors = array();
$accessor = $this->get('property_accessor');

foreach ($violations as $violation) {
    $accessor->setValue($errors, $violation->getPropertyPath(), $violation->getMessage());
}

=> array(
    'email' => 'This value should not be blank.',
    'password' => 'This value should have 6 characters or more.',
)

这也适用于多维数据数组。在那里,属性路径将类似于[author][name]。PropertyAccessor将在$errors数组中的相同位置插入错误消息,即$errors['author']['name'] = 'Message'

票数 5
EN

Stack Overflow用户

发布于 2019-05-28 20:27:59

PropertyAccessor's setValue没有真正的帮助,因为它不能处理单个字段的多个违规。例如,字段可能比约束长度短,并且还包含非法字符。为此,我们将收到两条错误消息。

我不得不创建自己的代码:

代码语言:javascript
复制
$messages = [];

foreach ($violations as $violation) {

    $field = substr($violation->getPropertyPath(), 1, -1);
    $messages[] = [$field => $violation->getMessage()];
}

$output = [
    'name' => array_unique(array_column($messages, 'name')),
    'email' => array_unique(array_column($messages, 'email')),
];

return $output;

我们手动从属性路径中剥离[]字符,并创建一个字段数组和相应消息的数组。稍后,我们将数组转换为按字段对消息进行分组。

代码语言:javascript
复制
$session = $request->getSession();
$session->getFlashBag()->setAll($messages);

在控制器中,我们将消息添加到flash包中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8437343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档