Created
September 26, 2017 10:50
-
-
Save teklakct/d372b7a1806d6f721f63a7fd3e7bb991 to your computer and use it in GitHub Desktop.
Symfony callback validator
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
<?php | |
namespace Acme; | |
use Symfony\Component\Validator\Constraints\NotBlank; | |
use Symfony\Component\Validator\Context\ExecutionContextInterface; | |
class CommandValidator | |
{ | |
public static function validate($object, ExecutionContextInterface $context) | |
{ | |
$notBlank = new NotBlank(); | |
$context->buildViolation($notBlank->message) | |
->atPath('realizationMethod') | |
->setCode('my_awesome_code | |
->addViolation() | |
; | |
} | |
} |
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
{ | |
"errors":[ | |
{ | |
"code":"callback", | |
"status":400, | |
"title":"Ta wartość nie powinna być pusta.", | |
"source":{ | |
"pointer":"/data/attributes/realizationMethod" | |
} | |
} | |
], | |
} |
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
{ | |
"errors":[ | |
{ | |
"code":"not_blank_is_blank_error", | |
"status":400, | |
"title":"Ta wartość nie powinna być pusta.", | |
"source":{ | |
"pointer":"/data/attributes/realizationMethod" | |
} | |
} | |
], | |
} |
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
AddCommand: | |
constraints: | |
- Callback: [Acme\CommandValidator, validate] | |
RemoveCommand: | |
properties: | |
realizationMethod: | |
- NotBlank: ~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment