Skip to content

Commit

Permalink
[Validator] Removed return value from ConstraintValidatorInterface::i…
Browse files Browse the repository at this point in the history
…sValid()
  • Loading branch information
webmozart committed Apr 17, 2012
1 parent e7470ff commit 46f0393
Show file tree
Hide file tree
Showing 64 changed files with 302 additions and 437 deletions.
Expand Up @@ -40,8 +40,6 @@ public function __construct(ManagerRegistry $registry)
/**
* @param object $entity
* @param Constraint $constraint
*
* @return bool
*/
public function isValid($entity, Constraint $constraint)
{
Expand Down Expand Up @@ -74,7 +72,7 @@ public function isValid($entity, Constraint $constraint)
$criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);

if (null === $criteria[$fieldName]) {
return true;
return;
}

if ($class->hasAssociation($fieldName)) {
Expand Down Expand Up @@ -113,11 +111,9 @@ public function isValid($entity, Constraint $constraint)
* unique.
*/
if (0 === count($result) || (1 === count($result) && $entity === ($result instanceof \Iterator ? $result->current() : current($result)))) {
return true;
return;
}

$this->context->addViolationAtSubPath($fields[0], $constraint->message, array(), $criteria[$fields[0]]);

return true; // all true, we added the violation already!
}
}
Expand Up @@ -41,10 +41,6 @@ public function isValid($password, Constraint $constraint)

if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
$this->context->addViolation($constraint->message);

return false;
}

return true;
}
}
Expand Up @@ -31,8 +31,6 @@ function initialize(ExecutionContext $context);
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constrain for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
function isValid($value, Constraint $constraint);
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class AllValidator extends ConstraintValidator
Expand All @@ -26,14 +28,12 @@ class AllValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
{
if (null === $value) {
return true;
return;
}

if (!is_array($value) && !$value instanceof \Traversable) {
Expand All @@ -53,7 +53,5 @@ public function isValid($value, Constraint $constraint)
$walker->walkConstraint($constr, $element, $group, $propertyPath.'['.$key.']');
}
}

return true;
}
}
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Validator\ConstraintValidator;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class BlankValidator extends ConstraintValidator
Expand All @@ -25,18 +27,12 @@ class BlankValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
{
if ('' !== $value && null !== $value) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));

return false;
}

return true;
}
}
Expand Up @@ -19,7 +19,7 @@
/**
* Validator for Callback constraint
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
Expand All @@ -31,14 +31,12 @@ class CallbackValidator extends ConstraintValidator
* @param mixed $object The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($object, Constraint $constraint)
{
if (null === $object) {
return true;
return;
}

// has to be an array so that we can differentiate between callables
Expand All @@ -48,24 +46,21 @@ public function isValid($object, Constraint $constraint)
}

$methods = $constraint->methods;
$success = true;

foreach ($methods as $method) {
if (is_array($method) || $method instanceof \Closure) {
if (!is_callable($method)) {
throw new ConstraintDefinitionException(sprintf('"%s::%s" targeted by Callback constraint is not a valid callable', $method[0], $method[1]));
}

$success = call_user_func($method, $object, $this->context) && $success;
call_user_func($method, $object, $this->context);
} else {
if (!method_exists($object, $method)) {
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist', $method));
}

$success = $object->$method($this->context) && $success;
$object->$method($this->context);
}
}

return $success;
}
}
16 changes: 4 additions & 12 deletions src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org>
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
Expand All @@ -33,8 +33,6 @@ class ChoiceValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
Expand All @@ -44,7 +42,7 @@ public function isValid($value, Constraint $constraint)
}

if (null === $value) {
return true;
return;
}

if ($constraint->multiple && !is_array($value)) {
Expand All @@ -67,8 +65,6 @@ public function isValid($value, Constraint $constraint)
foreach ($value as $_value) {
if (!in_array($_value, $choices, $constraint->strict)) {
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));

return false;
}
}

Expand All @@ -77,20 +73,16 @@ public function isValid($value, Constraint $constraint)
if ($constraint->min !== null && $count < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);

return false;
return;
}

if ($constraint->max !== null && $count > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);

return false;
return;
}
} elseif (!in_array($value, $choices, $constraint->strict)) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));

return false;
}

return true;
}
}
Expand Up @@ -18,6 +18,8 @@
use Symfony\Component\Validator\Constraints\Collection\Required;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class CollectionValidator extends ConstraintValidator
Expand All @@ -28,14 +30,12 @@ class CollectionValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
{
if (null === $value) {
return true;
return;
}

if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
Expand All @@ -46,8 +46,6 @@ public function isValid($value, Constraint $constraint)
$group = $this->context->getGroup();
$propertyPath = $this->context->getPropertyPath();

$valid = true;

foreach ($constraint->fields as $field => $fieldConstraint) {
if (
// bug fix issue #2779
Expand All @@ -71,7 +69,6 @@ public function isValid($value, Constraint $constraint)
$this->context->addViolationAtSubPath('['.$field.']', $constraint->missingFieldsMessage, array(
'{{ field }}' => $field
), null);
$valid = false;
}
}

Expand All @@ -81,11 +78,8 @@ public function isValid($value, Constraint $constraint)
$this->context->addViolationAtSubPath('['.$field.']', $constraint->extraFieldsMessage, array(
'{{ field }}' => $field
), $fieldValue);
$valid = false;
}
}
}

return $valid;
}
}
10 changes: 2 additions & 8 deletions src/Symfony/Component/Validator/Constraints/CountryValidator.php
Expand Up @@ -18,7 +18,7 @@
/**
* Validates whether a value is a valid country code
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
Expand All @@ -30,14 +30,12 @@ class CountryValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return true;
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
Expand All @@ -48,10 +46,6 @@ public function isValid($value, Constraint $constraint)

if (!in_array($value, \Symfony\Component\Locale\Locale::getCountries())) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));

return false;
}

return true;
}
}
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Validator\Constraints;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class DateTimeValidator extends DateValidator
Expand Down
16 changes: 4 additions & 12 deletions src/Symfony/Component/Validator/Constraints/DateValidator.php
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class DateValidator extends ConstraintValidator
Expand All @@ -28,18 +30,12 @@ class DateValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return true;
}

if ($value instanceof \DateTime) {
return true;
if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
Expand All @@ -50,10 +46,6 @@ public function isValid($value, Constraint $constraint)

if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));

return false;
}

return true;
}
}
10 changes: 3 additions & 7 deletions src/Symfony/Component/Validator/Constraints/EmailValidator.php
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class EmailValidator extends ConstraintValidator
Expand All @@ -26,14 +28,12 @@ class EmailValidator extends ConstraintValidator
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function isValid($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return true;
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
Expand Down Expand Up @@ -61,11 +61,7 @@ public function isValid($value, Constraint $constraint)

if (!$valid) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));

return false;
}

return true;
}

/**
Expand Down

0 comments on commit 46f0393

Please sign in to comment.