Symfony 2 Validator - 如何返回一个数组 [英] Symfony 2 Validator - how to return an array

查看:20
本文介绍了Symfony 2 Validator - 如何返回一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 Symfony2 Validator 来返回一个数组而不是一个对象.

I need the Symfony2 Validator to return an array rather than an object.

就像这样:

$insert = new MyEntity();
$insert->setTest1( 'testtesttest' );
$validator = $this->get('validator');
$errors = $validator->validate($insert);

...将启用此功能:

$errors[0]['message'] = "The email is not valid"

只是一个简单的数组,因为解析返回的对象非常困难.

Just a simple array as parsing the object returned is very difficult.

我了解验证器配置,但我只需要验证器返回一个数组而不是其通常的对象.

I understand the validator config, but I just need the Validator to return an array not its usual object.

我正在对结果进行 JSON 编码并且 (a) json_encode 与对象斗争 + (b) 我不想返回整个对象只是一个错误列表.

I'm JSON encoding the result and (a) json_encode struggles with objects + (b) I don't want to return the whole object just a list of errors.

我没有使用内置表单,只使用原始验证器.

I'm not using the in-built forms, just the raw Validator.

推荐答案

Validator->validate() 返回一个 ConstraintViolationListInterface,它实现了 IteratorAggregate 接口.简单的 foreach 并从 ConstraintViolationInterface 对象.

这篇关于Symfony 2 Validator - 如何返回一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆