use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
+use Epnadmin\CoreBundle\Validator\Constraints as EPNAssert;
+
/**
* @ORM\Entity(repositoryClass="ResourceRepository")
* @ORM\InheritanceType(value="JOINED")
* "computer" = "ResourceComputer",
* "zone" = "ResourceZone"
* })
+ *
+ * @EPNAssert\ValidParent(all=true,groups={"as_user"})
*/
abstract class Resource
{
/**
* @ORM\Column(type="string", length=100)
- * @Assert\NotBlank
+ * @Assert\NotBlank(groups={"as_user", "as_admin", "Default"})
*/
protected $name;
<argument type="service" id="doctrine" />
<tag name="form.type" alias="epn_resource_choice" />
</service>
+
+ <service id="epnadmincore.services.resource_valid_parent_validator" class="Epnadmin\CoreBundle\Validator\Constraints\ValidParentValidator">
+ <argument type="service" id="doctrine" />
+ <tag name="validator.constraint_validator" alias="resource_valid_parent_validator" />
+ </service>
</services>
</container>
--- /dev/null
+<?php
+
+namespace Epnadmin\CoreBundle\Validator\Constraints;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ */
+class ValidParent extends Constraint
+{
+ public $all = false;
+ public $message_parent_inactive = 'Le parent "%name%" n\'est pas activé';
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getTargets()
+ {
+ return self::CLASS_CONSTRAINT;
+ }
+
+ public function validatedBy()
+ {
+ return 'resource_valid_parent_validator';
+ }
+}
--- /dev/null
+<?php
+
+namespace Epnadmin\CoreBundle\Validator\Constraints;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+use Symfony\Bridge\Doctrine\RegistryInterface;
+
+class ValidParentValidator extends ConstraintValidator
+{
+ public function __construct(RegistryInterface $doctrine)
+ {
+ $this->doctrine = $doctrine;
+ }
+
+ public function validate($value, Constraint $constraint)
+ {
+ $parent = $value->getParent();
+
+ if (!$parent->isActive()) {
+ $this->context->addViolation($constraint->message_parent_inactive, array(
+ '%name%' => $parent->getName()
+ ));
+ }
+ }
+}