Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[Form] Allow setting custom message, adapt to sf2.1 API #188

Merged
merged 1 commit into from

3 participants

@marekkalnik

This PR allows setting the message by using 'message' option.

It contains also a refactoring to adapt the code to recent Symfony forms API changes (removes dependency on deprecated functions).

It fixes #160.

The PR should be safe to merge into 1.1 and 2.0 (do you want me to make a separate PR?).
Also note that the namespace is not conform with Symfony2 namespaces (should be Constraint rather than Constraints) but I didn't touch it to avoid BC breaks.

@willdurand
Owner

Perfect.

@willdurand willdurand merged commit 6444ffe into propelorm:1.1
@willdurand
Owner

Thank you!

@marekkalnik

No problem. You want me to make separate PRs for 2.0 and master or you'll merge this yourself ?

@willdurand
Owner
@marekkalnik

No, unfortunately only 1.1 ATM.

@EdwinLove

Any idea when Packagist will pick up the 1.1.4 tag, am keen to get on working with it.....

@willdurand
Owner

Done :)

@EdwinLove

Many thanks :D

@EdwinLove

I think the original namespacing was correct.
Symfony uses Constraints not Constrain

@marekkalnik

Argh, you're right! My mistake!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
51 Validator/Constraints/UniqueObject.php
@@ -1,5 +1,4 @@
<?php
-
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
@@ -11,60 +10,58 @@
namespace Propel\PropelBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
-use Symfony\Component\Validator\ConstraintValidator;
+use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Constraint for the Unique Object validator
*
* @author Maxime AILLOUD <maxime.ailloud@gmail.com>
+ * @author Marek Kalnik <marekk@theodo.fr>
*/
class UniqueObject extends Constraint
{
/**
- * @var array
+ * @var string
*/
- public $fields = array();
+ public $message = 'A {{ object_class }} object already exists with {{ fields }}';
/**
- * @return array
+ * @var string Used to merge multiple fields in the message
*/
- public function getRequiredOptions()
- {
- return array('fields');
- }
+ public $messageFieldSeparator = ' and ';
/**
- * The validator must be defined as a service with this name.
- *
- * @return string
+ * @var array
*/
- public function validatedBy()
- {
- return get_class($this).'Validator';
- }
+ public $fields = array();
- /**
- * {@inheritDoc}
- */
- public function getTargets()
+ public function __construct($options = null)
{
- return self::CLASS_CONSTRAINT;
+ parent::__construct($options);
+
+ if (!is_array($this->fields) && !is_string($this->fields)) {
+ throw new UnexpectedTypeException($this->fields, 'array');
+ }
+
+ if (0 === count($this->fields)) {
+ throw new ConstraintDefinitionException("At least one field must be specified.");
+ }
}
/**
- * @return string
+ * {@inheritDoc}
*/
- public function getDefaultOption()
+ public function getRequiredOptions()
{
- return 'fields';
+ return array('fields');
}
/**
- * @return string
+ * {@inheritDoc}
*/
- public function getMessage()
+ public function getTargets()
{
- return 'A ' . $this->groups[1] . ' object already exists';
+ return self::CLASS_CONSTRAINT;
}
}
View
46 Validator/Constraints/UniqueObjectValidator.php
@@ -1,5 +1,4 @@
<?php
-
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
@@ -13,31 +12,21 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
-use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Unique Object Validator checks if one or a set of fields contain unique values.
*
* @author Maxime AILLOUD <maxime.ailloud@gmail.com>
+ * @author Marek Kalnik <marekk@theodo.fr>
*/
class UniqueObjectValidator extends ConstraintValidator
{
/**
- * @param object $object
- * @param \Symfony\Component\Validator\Constraint $constraint
- * @return Boolean
+ * {@inheritdoc}
*/
- public function isValid($object, Constraint $constraint)
+ public function validate($object, Constraint $constraint)
{
- if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
- throw new UnexpectedTypeException($constraint->fields, 'array');
- }
-
- $fields = (array)$constraint->fields;
-
- if (0 === count($fields)) {
- throw new ConstraintDefinitionException("At least one field must be specified.");
- }
+ $fields = (array) $constraint->fields;
$class = get_class($object);
$peerClass = $class . 'Peer';
@@ -52,26 +41,33 @@ public function isValid($object, Constraint $constraint)
$bddUsersQuery = $queryClass::create();
foreach ($fields as $fieldName) {
- $bddUsersQuery->filterBy($peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME), $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME));
+ $bddUsersQuery->filterBy(
+ $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME),
+ $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME)
+ );
}
$bddUsers = $bddUsersQuery->find();
$countUser = count($bddUsers);
if ($countUser > 1 || ($countUser === 1 && $object !== $bddUsers[0])) {
- $constraintMessage = $constraint->getMessage();
- $constraintMessage .= ' with';
+ $fieldParts = array();
foreach ($fields as $fieldName) {
- $constraintMessage .= sprintf(' %s "%s" and', $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME), $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME));
+ $fieldParts[] = sprintf(
+ '%s "%s"',
+ $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME),
+ $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME)
+ );
}
- $constraintMessage = substr($constraintMessage, 0, -4) . '.';
- $this->setMessage($constraintMessage);
-
- return false;
+ $this->context->addViolation(
+ $constraint->message,
+ array(
+ '{{ object_class }}' => $class,
+ '{{ fields }}' => implode($constraint->messageFieldSeparator, $fieldParts)
+ )
+ );
}
-
- return true;
}
}
Something went wrong with that request. Please try again.