Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. class ChildCategoryHasItsOwnPageValidator extends ConstraintValidator
  2. {
  3. protected $doctrine;
  4.  
  5. public function __construct(RegistryInterface $doctrine)
  6. {
  7. $this->doctrine = $doctrine;
  8. }
  9.  
  10.  
  11. public function validate($value, Constraint $constraint)
  12. {
  13. //...
  14. }
  15. }
  16.  
  17. PlacasFrontendBundleEntityCategory:
  18. properties:
  19. ownPage:
  20. - PlacasFrontendBundleValidatorConstraintsChildCategoryHasItsOwnPage: ~
  21.  
  22. /**
  23. * @Annotation
  24. */
  25. class ChildCategoryHasItsOwnPage extends Constraint
  26. {
  27. public $message = 'This category has a child category with an own page. You can not define an own page for this category.';
  28.  
  29. public function validatedBy()
  30. {
  31. return "child_category_has_its_own_page";
  32. }
  33. }
  34.  
  35. PlacasFrontendBundleEntityCategory:
  36. properties:
  37. ownPage:
  38. - PlacasFrontendBundleValidatorConstraintsChildCategoryHasItsOwnPage: ~
  39.  
  40. PlacasFrontendBundleEntityCategory:
  41. constraints:
  42. - Callback: [validate]
  43.  
  44. use SymfonyComponentValidatorExecutionContextInterface;
  45.  
  46. class Category
  47. {
  48. public function validate(ExecutionContextInterface $context)
  49. {
  50. $children = $this->getChildren(); // assuming you have this relationship and its getter
  51. foreach ($children as $child)
  52. {
  53. if ($child->foo != '') // or whatever test you want to make
  54. {
  55. $context->addViolationAt(
  56. 'foo',
  57. 'This category has a child category with an own page. You can not define an own page for this category.',
  58. array(),
  59. null
  60. );
  61. }
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement