Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function isAuthorValid(ExecutionContextInterface $context)
- {
- $original = ... ; // get old values
- if( $this->status !== $original->status && $this->status === 'Racked' && $original->status === 'Unracked' )
- {
- // check ftp and ssh connection
- // $context->addViolationAt('status', 'Unable to connect etc etc');
- }
- }
- namespace VendorYourBundleValidationConstraints;
- use SymfonyComponentDependencyInjectionContainerInterface;
- use SymfonyComponentValidatorConstraint;
- use SymfonyComponentValidatorConstraintValidator;
- class StatusValidator extends ConstraintValidator
- {
- protected $container;
- public function __construct(ContainerInterface $container)
- {
- $this->container = $container;
- }
- public function validate($status, Constraint $constraint)
- {
- $em = $this->container->get('doctrine')->getEntityManager('default');
- $previousStatus = $em->getRepository('YourBundle:Status')->findOneBy(array('id' => $status->getId()));
- // ... do something with the previous status here
- if ( $previousStatus->getValue() != $status->getValue() ) {
- $this->context->addViolationAt('whatever', $constraint->message, array(), null);
- }
- }
- public function getTargets()
- {
- return self::CLASS_CONSTRAINT;
- }
- public function validatedBy()
- {
- return 'previous_value';
- }
- }
- services:
- validator.previous_value:
- class: VendorYourBundleValidationConstraintsStatusValidator
- # example! better inject only the services you need ...
- # i.e. ... @doctrine.orm.entity_manager
- arguments: [ @service_container ]
- tags:
- - { name: validator.constraint_validator, alias: previous_value }
- use VendorYourBundleValidationConstraints as MyValidation;
- /**
- * @MyValidationStatusValidator
- */
- class Status
- {
- // src/Acme/AcmeBundle/Validator/Constraints/IncrementOnly.php;
- <?php
- namespace AcmeAcmeBundleValidatorConstraints;
- use SymfonyComponentValidatorConstraint;
- /**
- * @Annotation
- */
- class IncrementOnly extends Constraint
- {
- public $message = 'The new value %new% is least than the old %old%';
- public function getTargets()
- {
- return self::CLASS_CONSTRAINT;
- }
- public function validatedBy()
- {
- return 'increment_only';
- }
- }
- // src/Acme/AcmeBundle/Validator/Constraints/IncrementOnlyValidator.php
- <?php
- namespace AcmeAcmeBundleValidatorConstraints;
- use SymfonyComponentValidatorConstraint;
- use SymfonyComponentValidatorConstraintValidator;
- use DoctrineORMEntityManager;
- class IncrementOnlyValidator extends ConstraintValidator
- {
- protected $em;
- public function __construct(EntityManager $em)
- {
- $this->em = $em;
- }
- public function validate($object, Constraint $constraint)
- {
- $new_value = $object->getIntegerField();
- $old_data = $this->em
- ->getUnitOfWork()
- ->getOriginalEntityData($object);
- // $old_data is empty if we create a new NoDecreasingInteger object.
- if (is_array($old_data) and !empty($old_data))
- {
- $old_value = $old_data['integerField'];
- if ($new_value < $old_value)
- {
- $this->context->buildViolation($constraint->message)
- ->setParameter("%new%", $new_value)
- ->setParameter('%old%', $old_value)
- ->addViolation();
- }
- }
- }
- }
- // src/Acme/AcmeBundle/Resources/config/validator.yml
- AcmeAcmeBundleEntityNoDecreasingInteger:
- constraints:
- - AcmeAcmeBundleValidatorConstraintsIncrementOnly: ~
- // src/Acme/AcmeBundle/Resources/config/services.yml
- services:
- validator.increment_only:
- class: AcmeAcmeBundleValidatorConstraintsIncrementOnlyValidator
- arguments: ["@doctrine.orm.entity_manager"]
- tags:
- - { name: validator.constraint_validator, alias: increment_only }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement