Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator] add Validation::createCallable()
- Loading branch information
1 parent
1cfadb4
commit 2e4f2ac
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/Validator/Exception/ValidationFailedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\Validator\Exception; | ||
|
||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
|
||
/** | ||
* @author Jan Vernieuwe <jan.vernieuwe@phpro.be> | ||
*/ | ||
class ValidationFailedException extends RuntimeException | ||
{ | ||
private $violations; | ||
private $value; | ||
|
||
public function __construct($value, ConstraintViolationListInterface $violations) | ||
{ | ||
$this->violations = $violations; | ||
$this->value = $value; | ||
parent::__construct($violations); | ||
} | ||
|
||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function getViolations(): ConstraintViolationListInterface | ||
{ | ||
return $this->violations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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\Validator\Tests\Validator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Constraints\Email; | ||
use Symfony\Component\Validator\Exception\ValidationFailedException; | ||
use Symfony\Component\Validator\Validation; | ||
|
||
/** | ||
* @author Jan Vernieuwe <jan.vernieuwe@phpro.be> | ||
*/ | ||
class ValidationTest extends TestCase | ||
{ | ||
public function testCreateCallableValid() | ||
{ | ||
$validator = Validation::createCallable(new Email()); | ||
$this->assertEquals('test@example.com', $validator('test@example.com')); | ||
} | ||
|
||
public function testCreateCallableInvalid() | ||
{ | ||
$validator = Validation::createCallable(new Email()); | ||
$this->expectException(ValidationFailedException::class); | ||
$validator('test'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters