Skip to content

Commit

Permalink
[Validator] fixed Min and Max validator when the input value is not a…
Browse files Browse the repository at this point in the history
… number (now return an error message instead of an exception which does not make sense in this context)
  • Loading branch information
fabpot committed Jul 12, 2011
1 parent 3ca536e commit 88d915d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Max.php
Expand Up @@ -17,6 +17,7 @@
class Max extends Constraint
{
public $message = 'This value should be {{ limit }} or less';
public $invalidMessage = 'This value should be a valid number';

This comment has been minimized.

Copy link
@vicb

vicb Jul 12, 2011

Contributor

Should this string be added to the translation files ?

This comment has been minimized.

Copy link
@stloyd

stloyd Jul 12, 2011

Contributor

AFAIK should be, as its returned to the user as error.

This comment has been minimized.

Copy link
@jmikola

jmikola Jul 18, 2011

Contributor

Hypothetically, what happens if this message triggers for both Max and Min constraints on the same field? Would it be duplicated in the frontend?

public $limit;

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Validator/Constraints/MaxValidator.php
Expand Up @@ -24,7 +24,12 @@ public function isValid($value, Constraint $constraint)
}

if (!is_numeric($value)) {
throw new UnexpectedTypeException($value, 'numeric');
$this->setMessage($constraint->invalidMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->limit,
));

return false;
}

if ($value > $constraint->limit) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Min.php
Expand Up @@ -17,6 +17,7 @@
class Min extends Constraint
{
public $message = 'This value should be {{ limit }} or more';
public $invalidMessage = 'This value should be a valid number';
public $limit;

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Validator/Constraints/MinValidator.php
Expand Up @@ -24,7 +24,12 @@ public function isValid($value, Constraint $constraint)
}

if (!is_numeric($value)) {
throw new UnexpectedTypeException($value, 'numeric');
$this->setMessage($constraint->invalidMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->limit,
));

return false;
}

if ($value < $constraint->limit) {
Expand Down
Expand Up @@ -33,13 +33,6 @@ public function testNullIsValid()
$this->assertTrue($this->validator->isValid(null, new Max(array('limit' => 10))));
}

public function testExpectsNumericType()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

$this->validator->isValid(new \stdClass(), new Max(array('limit' => 10)));
}

/**
* @dataProvider getValidValues
*/
Expand Down Expand Up @@ -73,6 +66,7 @@ public function getInvalidValues()
return array(
array(10.00001),
array('10.00001'),
array(new \stdClass()),
);
}

Expand Down
Expand Up @@ -28,13 +28,6 @@ public function testNullIsValid()
$this->assertTrue($this->validator->isValid(null, new Min(array('limit' => 10))));
}

public function testExpectsNumericType()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

$this->validator->isValid(new \stdClass(), new Min(array('limit' => 10)));
}

/**
* @dataProvider getValidValues
*/
Expand Down Expand Up @@ -68,6 +61,7 @@ public function getInvalidValues()
return array(
array(9.999999),
array('9.999999'),
array(new \stdClass()),
);
}

Expand Down

1 comment on commit 88d915d

@jalliot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the "use" statement for UnexpectedTypeException.

Please sign in to comment.