-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests; | ||
|
||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\Form\FormBuilder; | ||
use Symfony\Component\Form\FormError; | ||
use Symfony\Component\Form\FormErrorIterator; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
|
||
class FormErrorIteratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider findByCodesProvider | ||
*/ | ||
public function testFindByCodes($code, $violationsCount) | ||
{ | ||
if (!class_exists(ConstraintViolation::class)) { | ||
$this->markTestSkipped('Validator component required.'); | ||
} | ||
|
||
$formBuilder = new FormBuilder( | ||
'form', | ||
null, | ||
new EventDispatcher(), | ||
$this->getMock('Symfony\Component\Form\FormFactoryInterface'), | ||
array() | ||
); | ||
|
||
$form = $formBuilder->getForm(); | ||
|
||
$cause = new ConstraintViolation('Error 1!', null, array(), null, '', null, null, 'code1'); | ||
$form->addError(new FormError('Error 1!', null, array(), null, $cause)); | ||
$cause = new ConstraintViolation('Error 2!', null, array(), null, '', null, null, 'code1'); | ||
$form->addError(new FormError('Error 2!', null, array(), null, $cause)); | ||
$cause = new ConstraintViolation('Error 3!', null, array(), null, '', null, null, 'code2'); | ||
$form->addError(new FormError('Error 3!', null, array(), null, $cause)); | ||
$formErrors = $form->getErrors(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps should be tested with nested errors. |
||
|
||
$specificFormErrors = $formErrors->findByCodes($code); | ||
$this->assertInstanceOf(FormErrorIterator::class, $specificFormErrors); | ||
$this->assertCount($violationsCount, $specificFormErrors); | ||
} | ||
|
||
public function findByCodesProvider() | ||
{ | ||
return array( | ||
array('code1', 2), | ||
array(array('code1', 'code2'), 3), | ||
array('code3', 0), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,4 +158,24 @@ public function offsetUnset($offset) | |
{ | ||
$this->remove($offset); | ||
} | ||
|
||
/** | ||
* Creates iterator for errors with specific codes. | ||
* | ||
* @param string|string[] $codes The codes to find | ||
* | ||
* @return static New instance which contains only specific errors. | ||
*/ | ||
public function findByCodes($codes) | ||
{ | ||
$codes = (array) $codes; | ||
$violations = array(); | ||
foreach ($this as $violation) { | ||
if (in_array($violation->getCode(), $codes, true)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Personally i'd favor API like final public function findByCode($code)
{
return $this->findByCodes(array($code));
}
public function findByCodes(array $codes)
{ } But this is fine 👍 |
||
$violations[] = $violation; | ||
} | ||
} | ||
|
||
return new static($violations); | ||
} | ||
} |
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 be
findByCause
..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 method
findByCause
- too much code for writing closure. It equals to writing foreach.