我正在验证一些值:
$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:
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函数也是无用的。
"[email]: This value should not be blank","[password]: This value should not be blank"发布于 2012-05-30 03:00:36
属性路径可以映射到属性,也可以映射到索引。考虑一个实现\ArrayAccess的类OptionBag和一个方法getSize()。
属性路径[size]引用$optionBag->getSize()
size引用[size]引用$optionBag['size']在本例中,您将验证一个数组。由于还可以通过索引访问数组元素,因此冲突中产生的属性路径包含方括号。
更新:
您不必手动删除方括号。您可以使用Symfony的PropertyAccess组件将错误映射到与您的数据具有相同结构的数组,例如:
$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'。
发布于 2019-05-28 20:27:59
PropertyAccessor's setValue没有真正的帮助,因为它不能处理单个字段的多个违规。例如,字段可能比约束长度短,并且还包含非法字符。为此,我们将收到两条错误消息。
我不得不创建自己的代码:
$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;我们手动从属性路径中剥离[]字符,并创建一个字段数组和相应消息的数组。稍后,我们将数组转换为按字段对消息进行分组。
$session = $request->getSession();
$session->getFlashBag()->setAll($messages);在控制器中,我们将消息添加到flash包中。
https://stackoverflow.com/questions/8437343
复制相似问题