Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DX][Form][Validator] Add ability check if cocrete constraint fails.#19496
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| } | ||
| } | ||
| publicfunctiongetFormErrorByCode($code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What abouthasErrorCode? And what if there are many errors with the same code?
I suggest either to return a boolean: false => no such error, true => error(s), or empty array/array of error(s).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@HeahDude I would prefer return array/empty array, what name give for this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Maybe just add an "s" to error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
FormErrorIterator::getFormErrorsByCode duplicates terms, the context is clear due the class name. We're dealing with form errors. Ie. you could go withFormErrorIterator::getByCode or maybefindByCode.
However if we can get anewFormErrorIterator instance for oneor more codes that would be truly convenient.. ie.$specificErrros = $errors->withCodes(array(Constraint::CODE, OtherConstraint::CODE));
For quick one-liners, you could consider addinghasCode.
Koc commentedAug 2, 2016
//cc@webmozart |
| { | ||
| $violations =array(); | ||
| foreach ($thisas$violation) { | ||
| if (in_array($violation->getCode(),$codes,true)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
$codes should be casted to array. Imo this may also be typehinted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@ro0NL I've added support passing code or array of codes. No need wrap single code with array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Personally i'd favor API like
finalpublicfunctionfindByCode($code){return$this->findByCodes(array($code));}publicfunctionfindByCodes(array$codes){ }
But this is fine 👍
c2e1323 to5eb0822Compare| */ | ||
| publicfunctionfindByCodes($codes) | ||
| { | ||
| $codes = (array)$codes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Could be$errors
| publicfunctionfindByCodes($codes) | ||
| { | ||
| $codes = (array)$codes; | ||
| $formErrors =array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should be$error
| * @param string|string[] $codes The codes to find | ||
| * | ||
| * @return static New instance which contains only specific errors. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
As this only works for errors caused by the validator component...im not sure coupling hardcoded is appropriate. It's convenient though.. but maybe this should befindByCause..
$errros->findByCause(function($cause)use ($codes) {return$causeinstanceof ConstraintViolation &&in_array($cause->getCode(), (array)$codes,true);});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I haven't see any benefits from public methodfindByCause - too much code for writing closure. It equals to writing foreach.
Koc commentedSep 28, 2016
@fabpot@webmozart tests pass, examples have updated |
| $form->addError(newFormError('Error 2!',null,array(),null,$cause)); | ||
| $cause =newConstraintViolation('Error 3!',null,array(),null,'',null,null,'code2'); | ||
| $form->addError(newFormError('Error 3!',null,array(),null,$cause)); | ||
| $formErrors =$form->getErrors(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Perhaps should be tested with nested errors.
fabpot commentedMar 22, 2017
Thank you@Koc. |
…straint fails. (Koc)This PR was merged into the 3.3-dev branch.Discussion----------[DX][Form][Validator] Add ability check if cocrete constraint fails.| Q | A || --- | --- || Branch? | master || Bug fix? | no || New feature? | yes || BC breaks? | no || Deprecations? | no || Tests pass? | wait for travis || Fixed tickets |#15154 || License | MIT || Doc PR | should open |Sometimes for big forms with multiple constraints we should handle some errors separately.``` php// when using validator$constraintViolations = $validator->validate(...);if (count($constraintViolations->findByCodes(UniqueEntity::NOT_UNIQUE_ERROR))) { // display some message or send email or etc}// when using formsif (count($form->getErrors()->findByCodes(UniqueEntity::NOT_UNIQUE_ERROR))) { // display some message or send email or etc}```This PR add some useful methods to handle this. Before we should iterate all failed constraints using foreach.Feel free to suggest better names for new methods.Commits-------29a3a7e Add ability retrieve errors by their code.
Uh oh!
There was an error while loading.Please reload this page.
Sometimes for big forms with multiple constraints we should handle some errors separately.
This PR add some useful methods to handle this. Before we should iterate all failed constraints using foreach.
Feel free to suggest better names for new methods.