27/01/2017 - SYMFONY
Aşağıdaki örneği kullanıp bir constraint validator yaratabilir, servis olarak ayarlayabilir ve içine değişken ejekte edebilirsiniz.
namespace Application\TestBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class Coordinate extends Constraint
{
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
public function validatedBy()
{
return 'application_coordinate_validator';
}
}
namespace Application\TestBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CoordinateValidator extends ConstraintValidator
{
private $postcodesApiUri;
public function __construct($postcodesApiUri)
{
$this->postcodesApiUri = $postcodesApiUri;
}
public function validate($property, Constraint $constraint)
{
// Do something ...
}
}
services:
application.validator.coordinate_validator:
class: Application\TestBundle\Validator\Constraints\CoordinateValidator
arguments:
- "http://api.myendpoint.com/postcodes/"
tags:
-
name: validator.constraint_validator
alias: application_coordinate_validator
# OR
# tags:
# - { name: validator.constraint_validator, alias: application_coordinate_validator }