5

我需要将类型的对象转换为ConstraintViolationListInterface单个异常以进行进一步的日志记录,其中消息是来自列表中每个约束违规的消息的串联,当验证失败时。

显然,我不能使用验证在每个包中重复一个 foreach 循环来实现这一点,因此我正在考虑再创建一个包,提供一个简单的服务,接受ConstraintViolationListInterface并返回一个异常。Symfony 中是否有针对此问题的标准解决方案?我需要编写此服务似乎很奇怪,这个问题对我来说似乎很常见。

4

3 回答 3

10

我也很惊讶 symfony 对此没有任何帮助,这就是我创建自定义异常的原因:

class ValidationException extends \Exception
{
    private $violations;

    public function __construct(array $violations)
    {
        $this->violations = $violations;
        parent::__construct('Validation failed.');
    }

    public function getMessages()
    {
        $messages = [];
        foreach ($this->violations as $paramName => $violationList) {
            foreach ($violationList as $violation) {
                $messages[$paramName][] = $violation->getMessage();
            }
        }
        return $messages;
    }

    public function getJoinedMessages()
    {
        $messages = [];
        foreach ($this->violations as $paramName => $violationList) {
            foreach ($violationList as $violation) {
                $messages[$paramName][] = $violation->getMessage();
            }
            $messages[$paramName] = implode(' ', $messages[$paramName]);
        }
        return $messages;
    }
}

此处提供所有代码。

我以另一种方式使用此异常:

try {
    $errors = $validator->validate(...);
    if (0 !== count($errors)) {
        throw new ValidationException($errors);
    }
} catch (ValidationException $e) {
    // Here you can obtain your validation errors. 
    var_dump($e->getMessages());
}
于 2017-12-19T10:35:42.427 回答
3

这个解决方案对我来说很好:

protected function violationsToArray(ConstraintViolationListInterface $violations)
{
    $messages = [];

    foreach ($violations as $constraint) {
        $prop = $constraint->getPropertyPath();
        $messages[$prop][] = $constraint->getMessage();
    }

    return $messages;
}

请注意,使用 $violations 数组键作为属性名称将不起作用:

    $messages = [];

    foreach ($violations as $prop => $constraint) {
        // $prop will not contain any value and this will not work as expected
        $messages[$prop][] = $constraint->getMessage();
    }
于 2019-07-26T16:30:19.463 回答
0

也许你可以像这样创建一个 ConstraintViolationsEvent :

namespace AppBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Validator\ConstraintViolationListInterface;

/**  
 * The order.placed event is dispatched each time an order is created
 * in the system.
 */
class ConstraintViolationsEvent extends Event
{
    const VIOLATIONS_DETECTED = 'constraint_violations.detected';

    protected $constraintViolationList;

    public function __construct(ConstraintViolationListInterface $constraintViolationList)
    {
        $this->constraintViolationList = $constraintViolationList;
    }

    public function getConstraintViolationList()
    {
        return $this->constraintViolationList;
    }
}

然后,您可以为此事件创建一个侦听器,并在此侦听器中,根据发现的所有违规行为创建您的异常。每次您会发现违规行为时,您只需在控制器中发送事件,如下所示:

class MyController extends Controller
{
    public function myFormAction(Request $request)
    {
        /** handle the request, get the form data, validate the form...etc. **/
        $event = new ConstraintViolationsEvent($constraintViolationList);
        $dispatcher->dispatch(ConstraintViolationsEvent::VIOLATIONS_DETECTED, $event);
    }
}

实际上,您可以在服务中管理 Exception 的创建并在侦听器中调用该服务。它是由你决定。

于 2017-12-19T10:20:26.800 回答