Added constraints for selectable attribute.

This commit is contained in:
Jan Böhmer 2019-08-12 21:47:25 +02:00
parent 408d98c6e1
commit 51be176418
12 changed files with 169 additions and 0 deletions

View file

@ -37,6 +37,7 @@ use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* The validator for the NoneOfItsChildren annotation.
@ -62,6 +63,11 @@ class NoneOfItsChildrenValidator extends ConstraintValidator
return;
}
//Check type of value. Validating only works for StructuralDBElements
if (!$value instanceof StructuralDBElement) {
throw new UnexpectedValueException($value, 'StructuralDBElement');
}
//Check if the object is assigned to itself
/** @var StructuralDBElement $entity */
$entity = $this->context->getObject();

View file

@ -0,0 +1,47 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* If a property is marked with this constraint, the choosen value (of type StructuralDBElement)
* must NOT be marked as not selectable.
*
* @package App\Validator\Constraints
* @Annotation
*/
class Selectable extends Constraint
{
public $message = 'validator.isSelectable';
}

View file

@ -0,0 +1,78 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Validator\Constraints;
use App\Entity\Base\StructuralDBElement;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* The validator for the Selectable constraint.
* @package App\Validator\Constraints
*/
class SelectableValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param \Symfony\Component\Validator\Constraint $constraint The constraint for the validation
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Selectable) {
throw new UnexpectedTypeException($constraint, Selectable::class);
}
// custom constraints should ignore null and empty values to allow
// other constraints (NotBlank, NotNull, etc.) take care of that
if (null === $value || '' === $value) {
return;
}
//Check type of value. Validating only works for StructuralDBElements
if (!$value instanceof StructuralDBElement) {
throw new UnexpectedValueException($value, 'StructuralDBElement');
}
//Check if the value is not selectable -> show error message then.
if ($value->isNotSelectable()) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ name }}', $value->getName())
->setParameter('{{ full_path }}', $value->getFullPath())
->addViolation();
}
}
}