8000 [Validator] Simplify adding of constraint violations · Issue #6138 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Validator] Simplify adding of constraint violations #6138
Closed
@webmozart

Description

@webmozart

Currently, ExecutionContextInterface::addViolation() has a lot of optional parameters:

public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null);
public function addViolationAt($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null);

After #6129, we need to add another parameter $translationDomain to that list. Instead of adding a new parameter, I propose to deprecate all parameters after $params and introduce a new method getViolationBuilder() so that we end up with the following API in 2.3 (when deprecation is over):

public function addViolation($message, array $params = array());
public function addViolationAt($subPath, $message, array $params = array());
public function getViolationBuilder($message);
public function getViolationBuilderAt($subPath, $message);

and used like this:

// simple violations remain the same
$context->addViolationAt('foo', 'Foo is invalid!');

// complex violations will be easier to read and write
$context->getViolationBuilderAt('foo', 'The value {{ foo }} is invalid!')
    ->setParam('{{ foo }}', 'bar')
    ->setTranslationDomain('validators/foo')
    ->setInvalidValue('bar')
    ->addViolation();

Depends on #6137 to be merged.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0