Skip to content
This repository
Browse code

[UniqueValidator] Allow setting custom message, adapt to sf2.1 API

  • Loading branch information...
commit 6444ffe51138064689561471fcf9e94d3ab1d0a6 1 parent c032f46
Marek Kalnik marekkalnik authored
51 Validator/Constraints/UniqueObject.php
... ... @@ -1,5 +1,4 @@
1 1 <?php
2   -
3 2 /**
4 3 * This file is part of the PropelBundle package.
5 4 * For the full copyright and license information, please view the LICENSE
@@ -11,60 +10,58 @@
11 10 namespace Propel\PropelBundle\Validator\Constraints;
12 11
13 12 use Symfony\Component\Validator\Constraint;
14   -use Symfony\Component\Validator\ConstraintValidator;
  13 +use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15 14 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
16 15
17 16 /**
18 17 * Constraint for the Unique Object validator
19 18 *
20 19 * @author Maxime AILLOUD <maxime.ailloud@gmail.com>
  20 + * @author Marek Kalnik <marekk@theodo.fr>
21 21 */
22 22 class UniqueObject extends Constraint
23 23 {
24 24 /**
25   - * @var array
  25 + * @var string
26 26 */
27   - public $fields = array();
  27 + public $message = 'A {{ object_class }} object already exists with {{ fields }}';
28 28
29 29 /**
30   - * @return array
  30 + * @var string Used to merge multiple fields in the message
31 31 */
32   - public function getRequiredOptions()
33   - {
34   - return array('fields');
35   - }
  32 + public $messageFieldSeparator = ' and ';
36 33
37 34 /**
38   - * The validator must be defined as a service with this name.
39   - *
40   - * @return string
  35 + * @var array
41 36 */
42   - public function validatedBy()
43   - {
44   - return get_class($this).'Validator';
45   - }
  37 + public $fields = array();
46 38
47   - /**
48   - * {@inheritDoc}
49   - */
50   - public function getTargets()
  39 + public function __construct($options = null)
51 40 {
52   - return self::CLASS_CONSTRAINT;
  41 + parent::__construct($options);
  42 +
  43 + if (!is_array($this->fields) && !is_string($this->fields)) {
  44 + throw new UnexpectedTypeException($this->fields, 'array');
  45 + }
  46 +
  47 + if (0 === count($this->fields)) {
  48 + throw new ConstraintDefinitionException("At least one field must be specified.");
  49 + }
53 50 }
54 51
55 52 /**
56   - * @return string
  53 + * {@inheritDoc}
57 54 */
58   - public function getDefaultOption()
  55 + public function getRequiredOptions()
59 56 {
60   - return 'fields';
  57 + return array('fields');
61 58 }
62 59
63 60 /**
64   - * @return string
  61 + * {@inheritDoc}
65 62 */
66   - public function getMessage()
  63 + public function getTargets()
67 64 {
68   - return 'A ' . $this->groups[1] . ' object already exists';
  65 + return self::CLASS_CONSTRAINT;
69 66 }
70 67 }
46 Validator/Constraints/UniqueObjectValidator.php
... ... @@ -1,5 +1,4 @@
1 1 <?php
2   -
3 2 /**
4 3 * This file is part of the PropelBundle package.
5 4 * For the full copyright and license information, please view the LICENSE
@@ -13,31 +12,21 @@
13 12 use Symfony\Component\Validator\Constraint;
14 13 use Symfony\Component\Validator\ConstraintValidator;
15 14 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16   -use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17 15
18 16 /**
19 17 * Unique Object Validator checks if one or a set of fields contain unique values.
20 18 *
21 19 * @author Maxime AILLOUD <maxime.ailloud@gmail.com>
  20 + * @author Marek Kalnik <marekk@theodo.fr>
22 21 */
23 22 class UniqueObjectValidator extends ConstraintValidator
24 23 {
25 24 /**
26   - * @param object $object
27   - * @param \Symfony\Component\Validator\Constraint $constraint
28   - * @return Boolean
  25 + * {@inheritdoc}
29 26 */
30   - public function isValid($object, Constraint $constraint)
  27 + public function validate($object, Constraint $constraint)
31 28 {
32   - if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
33   - throw new UnexpectedTypeException($constraint->fields, 'array');
34   - }
35   -
36   - $fields = (array)$constraint->fields;
37   -
38   - if (0 === count($fields)) {
39   - throw new ConstraintDefinitionException("At least one field must be specified.");
40   - }
  29 + $fields = (array) $constraint->fields;
41 30
42 31 $class = get_class($object);
43 32 $peerClass = $class . 'Peer';
@@ -52,26 +41,33 @@ public function isValid($object, Constraint $constraint)
52 41
53 42 $bddUsersQuery = $queryClass::create();
54 43 foreach ($fields as $fieldName) {
55   - $bddUsersQuery->filterBy($peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME), $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME));
  44 + $bddUsersQuery->filterBy(
  45 + $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME),
  46 + $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME)
  47 + );
56 48 }
57 49 $bddUsers = $bddUsersQuery->find();
58 50
59 51 $countUser = count($bddUsers);
60 52
61 53 if ($countUser > 1 || ($countUser === 1 && $object !== $bddUsers[0])) {
62   - $constraintMessage = $constraint->getMessage();
63   - $constraintMessage .= ' with';
  54 + $fieldParts = array();
64 55
65 56 foreach ($fields as $fieldName) {
66   - $constraintMessage .= sprintf(' %s "%s" and', $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME), $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME));
  57 + $fieldParts[] = sprintf(
  58 + '%s "%s"',
  59 + $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME),
  60 + $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME)
  61 + );
67 62 }
68 63
69   - $constraintMessage = substr($constraintMessage, 0, -4) . '.';
70   - $this->setMessage($constraintMessage);
71   -
72   - return false;
  64 + $this->context->addViolation(
  65 + $constraint->message,
  66 + array(
  67 + '{{ object_class }}' => $class,
  68 + '{{ fields }}' => implode($constraint->messageFieldSeparator, $fieldParts)
  69 + )
  70 + );
73 71 }
74   -
75   - return true;
76 72 }
77 73 }

0 comments on commit 6444ffe

Please sign in to comment.
Something went wrong with that request. Please try again.