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

[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

Merged
fabpot merged 1 commit intosymfony:masterfromKoc:get-errors-by-code
Mar 22, 2017

Conversation

@Koc
Copy link
Contributor

@KocKoc commentedAug 1, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?wait for travis
Fixed tickets#15154
LicenseMIT
Doc PRshould open

Sometimes for big forms with multiple constraints we should handle some errors separately.

// 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.

ro0NL reacted with thumbs up emoji
@carsonbotcarsonbot added Status: Needs Review DXDX = Developer eXperience (anything that improves the experience of using Symfony) Form Validator Feature labelsAug 1, 2016
}
}

publicfunctiongetFormErrorByCode($code)
Copy link
Contributor

@HeahDudeHeahDudeAug 1, 2016
edited
Loading

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).

Copy link
ContributorAuthor

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?

Copy link
Contributor

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

Copy link
Contributor

@ro0NLro0NLAug 7, 2016
edited
Loading

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
Copy link
ContributorAuthor

Koc commentedAug 2, 2016

//cc@webmozart

{
$violations =array();
foreach ($thisas$violation) {
if (in_array($violation->getCode(),$codes,true)) {
Copy link
Contributor

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.

Copy link
ContributorAuthor

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

Copy link
Contributor

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 👍

@KocKocforce-pushed theget-errors-by-code branch 2 times, most recently fromc2e1323 to5eb0822CompareSeptember 24, 2016 10:18
*/
publicfunctionfindByCodes($codes)
{
$codes = (array)$codes;
Copy link
Contributor

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();
Copy link
Contributor

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.
*/
Copy link
Contributor

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);});

Copy link
ContributorAuthor

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
Copy link
ContributorAuthor

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();
Copy link
Contributor

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.

@nicolas-grekasnicolas-grekas added this to the3.x milestoneDec 6, 2016
@fabpot
Copy link
Member

Thank you@Koc.

@fabpotfabpot merged commit29a3a7e intosymfony:masterMar 22, 2017
fabpot added a commit that referenced this pull requestMar 22, 2017
…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.
@nicolas-grekasnicolas-grekas modified the milestones:3.x,3.3Mar 24, 2017
@fabpotfabpot mentioned this pull requestMay 1, 2017
@KocKoc deleted the get-errors-by-code branchJuly 22, 2019 12:08
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@ro0NLro0NLro0NL left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

DXDX = Developer eXperience (anything that improves the experience of using Symfony)FeatureFormStatus: Needs ReviewValidator

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

6 participants

@Koc@fabpot@ro0NL@HeahDude@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp