Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. public function isAuthorValid(ExecutionContextInterface $context)
  2. {
  3. $original = ... ; // get old values
  4. if( $this->status !== $original->status && $this->status === 'Racked' && $original->status === 'Unracked' )
  5. {
  6. // check ftp and ssh connection
  7. // $context->addViolationAt('status', 'Unable to connect etc etc');
  8. }
  9. }
  10.  
  11. namespace VendorYourBundleValidationConstraints;
  12.  
  13. use SymfonyComponentDependencyInjectionContainerInterface;
  14. use SymfonyComponentValidatorConstraint;
  15. use SymfonyComponentValidatorConstraintValidator;
  16.  
  17. class StatusValidator extends ConstraintValidator
  18. {
  19. protected $container;
  20.  
  21. public function __construct(ContainerInterface $container)
  22. {
  23. $this->container = $container;
  24. }
  25.  
  26. public function validate($status, Constraint $constraint)
  27. {
  28.  
  29. $em = $this->container->get('doctrine')->getEntityManager('default');
  30.  
  31. $previousStatus = $em->getRepository('YourBundle:Status')->findOneBy(array('id' => $status->getId()));
  32.  
  33. // ... do something with the previous status here
  34.  
  35. if ( $previousStatus->getValue() != $status->getValue() ) {
  36. $this->context->addViolationAt('whatever', $constraint->message, array(), null);
  37. }
  38. }
  39.  
  40. public function getTargets()
  41. {
  42. return self::CLASS_CONSTRAINT;
  43. }
  44.  
  45. public function validatedBy()
  46. {
  47. return 'previous_value';
  48. }
  49. }
  50.  
  51. services:
  52. validator.previous_value:
  53. class: VendorYourBundleValidationConstraintsStatusValidator
  54.  
  55. # example! better inject only the services you need ...
  56. # i.e. ... @doctrine.orm.entity_manager
  57.  
  58. arguments: [ @service_container ]
  59. tags:
  60. - { name: validator.constraint_validator, alias: previous_value }
  61.  
  62. use VendorYourBundleValidationConstraints as MyValidation;
  63.  
  64. /**
  65. * @MyValidationStatusValidator
  66. */
  67. class Status
  68. {
  69.  
  70. // src/Acme/AcmeBundle/Validator/Constraints/IncrementOnly.php;
  71. <?php
  72. namespace AcmeAcmeBundleValidatorConstraints;
  73.  
  74. use SymfonyComponentValidatorConstraint;
  75.  
  76. /**
  77. * @Annotation
  78. */
  79. class IncrementOnly extends Constraint
  80. {
  81. public $message = 'The new value %new% is least than the old %old%';
  82.  
  83. public function getTargets()
  84. {
  85. return self::CLASS_CONSTRAINT;
  86. }
  87.  
  88. public function validatedBy()
  89. {
  90. return 'increment_only';
  91. }
  92. }
  93.  
  94. // src/Acme/AcmeBundle/Validator/Constraints/IncrementOnlyValidator.php
  95. <?php
  96. namespace AcmeAcmeBundleValidatorConstraints;
  97.  
  98. use SymfonyComponentValidatorConstraint;
  99. use SymfonyComponentValidatorConstraintValidator;
  100.  
  101. use DoctrineORMEntityManager;
  102.  
  103. class IncrementOnlyValidator extends ConstraintValidator
  104. {
  105. protected $em;
  106.  
  107. public function __construct(EntityManager $em)
  108. {
  109. $this->em = $em;
  110. }
  111.  
  112. public function validate($object, Constraint $constraint)
  113. {
  114. $new_value = $object->getIntegerField();
  115.  
  116. $old_data = $this->em
  117. ->getUnitOfWork()
  118. ->getOriginalEntityData($object);
  119.  
  120. // $old_data is empty if we create a new NoDecreasingInteger object.
  121. if (is_array($old_data) and !empty($old_data))
  122. {
  123. $old_value = $old_data['integerField'];
  124.  
  125. if ($new_value < $old_value)
  126. {
  127. $this->context->buildViolation($constraint->message)
  128. ->setParameter("%new%", $new_value)
  129. ->setParameter('%old%', $old_value)
  130. ->addViolation();
  131. }
  132. }
  133. }
  134. }
  135.  
  136. // src/Acme/AcmeBundle/Resources/config/validator.yml
  137. AcmeAcmeBundleEntityNoDecreasingInteger:
  138. constraints:
  139. - AcmeAcmeBundleValidatorConstraintsIncrementOnly: ~
  140.  
  141. // src/Acme/AcmeBundle/Resources/config/services.yml
  142. services:
  143. validator.increment_only:
  144. class: AcmeAcmeBundleValidatorConstraintsIncrementOnlyValidator
  145. arguments: ["@doctrine.orm.entity_manager"]
  146. tags:
  147. - { name: validator.constraint_validator, alias: increment_only }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement