Skip to content

Instantly share code, notes, and snippets.

@teklakct
Created September 26, 2017 10:50
Show Gist options
  • Save teklakct/d372b7a1806d6f721f63a7fd3e7bb991 to your computer and use it in GitHub Desktop.
Save teklakct/d372b7a1806d6f721f63a7fd3e7bb991 to your computer and use it in GitHub Desktop.
Symfony callback validator
<?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()
;
}
}
{
"errors":[
{
"code":"callback",
"status":400,
"title":"Ta wartość nie powinna być pusta.",
"source":{
"pointer":"/data/attributes/realizationMethod"
}
}
],
}
{
"errors":[
{
"code":"not_blank_is_blank_error",
"status":400,
"title":"Ta wartość nie powinna być pusta.",
"source":{
"pointer":"/data/attributes/realizationMethod"
}
}
],
}
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