Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator] Add ConstraintViolationBuilder methods: fromViolation(), setPath(), getViolation()#60582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
rela589n wants to merge2 commits intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromrela589n:feat-constraint-violation-builder-from-violation

Conversation

rela589n
Copy link
Contributor

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
Issues#58029 (comment)
LicenseMIT

This PR: (1) adds the ability to create constraint violation builder from an existing violation (static factory method:ConstraintViolationBuilder::fromViolation($violation)) so that it can be adjusted in the builder, in particular (2)setPath() method, and finally retrieve new violation with (3)getViolation() method.

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Please add a line to the changelog of the component also

*
* @return $this
*
* @see \Symfony\Contracts\Translation\TranslatorInterface::trans()
*/
publicfunctionsetPlural(int$number):static;
publicfunctionsetPlural(?int$number):static;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this is a BC break
looking at the decoration test case, this might not be needed - instead, don't call setPlural if getPlural returns null

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

you're absolutely right, thank you for pointing this out
for some reason I didn't think about decoration 🫤

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I've changed the interface

What aboutConstarintViolationBuilder itself?

@@ -90,7 +120,7 @@ public function setInvalidValue(mixed $invalidValue): static
return$this;
}

publicfunctionsetPlural(int$number):static
publicfunctionsetPlural(?int$number):static

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

see my concern about this change in the interface

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

changed the interface

btw, from my point question arises: if at any point we'd like to extend interface, how is it handled?
is this change released on a new major version as bc break? is there any deprecation of "not using new type"?

@@ -27,24 +28,52 @@
class ConstraintViolationBuilderimplements ConstraintViolationBuilderInterface
{
privatestring$propertyPath;
private ?string$message =null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

no need to make this nullable:

Suggested change
private?string$message =null;
private string$translatedMessage;

Copy link
ContributorAuthor

@rela589nrela589nMay 31, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

is it the point that it will be eventually initialized?

right now this is only initialized as$this->message ??= $this->translateMessage()

any access to this property prior to that place will throw an exception

private ?Constraint$constraint,
privatestring|\Stringable$message,
privatestring|\Stringable$messageTemplate,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

let's keep the previous name

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I was motivated by using the same naming as inConstraintViolation, as these properties are named there as$message and$messageTemplate.

Naming them as$message and$translatedMessage here would confuse the picture, since$message means different things. Though, if you'd like them to be named this way, I will make a change.

Frankly speaking I myself thought about adding$translatedMessage, and letting the original$message alone, because the rename might introduce a BC break, as changing parameter name frommessage tomessageTemplate would break the invocation code if it passes it as a named parameter.

Though, I've checked BC promise, and it says:

[10] Parameter names are only covered by the compatibility promise for constructors of Attribute classes. Using PHP named arguments might break your code when upgrading to newer Symfony versions.

[11] Only optional argument(s) of a constructor at last position may be added.

So, keeping it unified looks better to me, but in any case, feel free to ask what you will

@rela589nrela589nforce-pushed thefeat-constraint-violation-builder-from-violation branch from4cf1543 to20f264bCompareMay 31, 2025 10:44
@rela589nrela589n changed the title[Validator] Add ConstraintViolationBuilderInterface methods: fromViolation(), setPath(), getViolation()[Validator] Add ConstraintViolationBuilder methods: fromViolation(), setPath(), getViolation()May 31, 2025
Copy link
Member

@xabbuhxabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't see that these are valuable changes as they change theConstraintViolationBuilder class in a way that makes it less obvious to use. Thus I am 👎 here.

private ?int$plural =null;
private ?string$code =null;
privatemixed$cause =null;

publicfunction__construct(
privateConstraintViolationList$violations,
private?ConstraintViolationListInterface$violations,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Making this property nullable doesn't look good to me as you would now have to be aware of the inner state of theConstraintViolationBuilder class to decide whether or not you can calladdViolation().

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@xabbuh , what do you propose instead?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

to decide whether or not you can call

how often is it necessary to "decide" in the way you say it?

afaik,addViolation is only called in custom validators that create violation builder using execution context, and it itself providesConstraintViolationList

So what is the point in "deciding"?

->setCause($violation->getCause());
}

publicfunctionsetPath(string$path):static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

having this method is probably confusing for users of this class given that we already have anatPath() method and by looking at the names it's not obvious how they differ.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

In most programming languages, setter methods (which modify private variables) are conventionally named by starting with "set" followed by the variable name, with the first letter of the variable name capitalized. For example, if a variable is named firstName, the setter would be setFirstName()

* objects.
*
* Use the various methods on this interface to configure the built violation.
* Finally, call {@link addViolation()} to add the violation to the current
* execution context.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @method ConstraintViolationInterface getViolation()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Adding this method adds a complete new meaning to the interface. I don't feel that it's a good idea having the same interface serve two different purposes.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@xabbuhxabbuhxabbuh requested changes

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

4 participants
@rela589n@nicolas-grekas@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp