Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 24 additions & 27 deletions 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
Expand All @@ -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;
}
}
46 changes: 21 additions & 25 deletions 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
Expand All @@ -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';
Expand All @@ -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;
}
}