Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChildCategoryHasItsOwnPageValidator extends ConstraintValidator
- {
- protected $doctrine;
- public function __construct(RegistryInterface $doctrine)
- {
- $this->doctrine = $doctrine;
- }
- public function validate($value, Constraint $constraint)
- {
- //...
- }
- }
- PlacasFrontendBundleEntityCategory:
- properties:
- ownPage:
- - PlacasFrontendBundleValidatorConstraintsChildCategoryHasItsOwnPage: ~
- /**
- * @Annotation
- */
- class ChildCategoryHasItsOwnPage extends Constraint
- {
- public $message = 'This category has a child category with an own page. You can not define an own page for this category.';
- public function validatedBy()
- {
- return "child_category_has_its_own_page";
- }
- }
- PlacasFrontendBundleEntityCategory:
- properties:
- ownPage:
- - PlacasFrontendBundleValidatorConstraintsChildCategoryHasItsOwnPage: ~
- PlacasFrontendBundleEntityCategory:
- constraints:
- - Callback: [validate]
- use SymfonyComponentValidatorExecutionContextInterface;
- class Category
- {
- public function validate(ExecutionContextInterface $context)
- {
- $children = $this->getChildren(); // assuming you have this relationship and its getter
- foreach ($children as $child)
- {
- if ($child->foo != '') // or whatever test you want to make
- {
- $context->addViolationAt(
- 'foo',
- 'This category has a child category with an own page. You can not define an own page for this category.',
- array(),
- null
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement