Skip to content

Commit

Permalink
Fix: stop on first field error
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Feb 15, 2021
1 parent 43a104a commit 4b4c40f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Phone/PhoneElement.php
Expand Up @@ -53,7 +53,7 @@ public function __construct(?ValueValidatorInterface $validator = null, ?Transfo
*/
protected function toPhp($httpValue)
{
if (empty($httpValue)) {
if ($httpValue === null) {
return null;
}

Expand Down
15 changes: 8 additions & 7 deletions src/Validator/ConstraintValueValidator.php
Expand Up @@ -54,16 +54,17 @@ public function validate($value, ElementInterface $element): FormError
}

$root = $element->root();
$groups = $root->constraintGroups();

/** @psalm-suppress TooManyArguments */
$errors = $root->getValidator()
->startContext($element)
->validate($value, $this->constraints, $root->constraintGroups())
->getViolations()
;
$context = $root->getValidator()->startContext($element);

if ($errors->has(0)) {
return FormError::violation($errors->get(0));
foreach ($this->constraints as $constraint) {
$errors = $context->validate($value, $constraint, $groups)->getViolations();

if ($errors->has(0)) {
return FormError::violation($errors->get(0));
}
}

return FormError::null();
Expand Down
12 changes: 12 additions & 0 deletions tests/Phone/PhoneElementTest.php
Expand Up @@ -75,6 +75,18 @@ public function test_submit_null()
$this->assertTrue($element->error()->empty());
}

/**
*
*/
public function test_submit_empty_string()
{
$element = new PhoneElement();

$this->assertTrue($element->submit('')->valid());
$this->assertSame('', $element->value()->getRawInput());
$this->assertTrue($element->error()->empty());
}

/**
*
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/Validator/ConstraintValueValidatorTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Bdf\Form\Validator;

use Bdf\Form\Constraint\Closure;
use Bdf\Form\Leaf\StringElement;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\NotBlank;
Expand Down Expand Up @@ -34,6 +35,40 @@ public function test_validate_without_constraints()
$this->assertTrue($validator->validate('value', $element)->empty());
}

/**
*
*/
public function test_validate_chain()
{
$calls = [];
$element = new StringElement();
$validator = new ConstraintValueValidator([
new Closure(function () use(&$calls) { $calls[] = 'A'; }),
new Closure(function () use(&$calls) { $calls[] = 'B'; }),
new Closure(function () use(&$calls) { $calls[] = 'C'; }),
]);

$this->assertTrue($validator->validate('value', $element)->empty());
$this->assertSame(['A', 'B', 'C'], $calls);
}

/**
*
*/
public function test_validate_chain_with_error_should_stop_on_first_error()
{
$calls = [];
$element = new StringElement();
$validator = new ConstraintValueValidator([
new Closure(function () use(&$calls) { $calls[] = 'A'; }),
new Closure(function () use(&$calls) { $calls[] = 'B'; return 'error'; }),
new Closure(function () use(&$calls) { $calls[] = 'C'; }),
]);

$this->assertEquals('error', $validator->validate('value', $element)->global());
$this->assertSame(['A', 'B'], $calls);
}

/**
*
*/
Expand Down

0 comments on commit 4b4c40f

Please sign in to comment.