Skip to content

Commit 19575ef

Browse files
committed
Merge pull request #1015 from feridmovsumov/master
Added new feature for nice error messages to ParamFetcher
2 parents 7ac33eb + d7e9e4b commit 19575ef

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

Request/ParamFetcher.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,8 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
210210
sprintf("%s parameter is an array", $paramType)
211211
);
212212
}
213-
214213
return $default;
215214
}
216-
217215
$constraint = new Regex(array(
218216
'pattern' => '#^'.$config->requirements.'$#xsu',
219217
'message' => sprintf(
@@ -223,10 +221,15 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
223221
$config->requirements
224222
),
225223
));
224+
} elseif (is_array($constraint) && isset($constraint["rule"]) && $constraint["error_message"]) {
225+
$constraint = new Regex(array(
226+
'pattern' => '#^'.$config->requirements["rule"].'$#xsu',
227+
'message' => $config->requirements["error_message"]
228+
));
229+
}
226230

227-
if (false === $config->allowBlank) {
228-
$constraint = array(new NotBlank(), $constraint);
229-
}
231+
if (false === $config->allowBlank) {
232+
$constraint = array(new NotBlank(), $constraint);
230233
}
231234

232235
if ($this->validator instanceof ValidatorInterface) {
@@ -237,9 +240,13 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
237240

238241
if (0 !== count($errors)) {
239242
if ($strict) {
240-
throw new BadRequestHttpException($this->violationFormatter->formatList($config, $errors));
243+
if (is_array($config->requirements) && isset($config->requirements["error_message"])) {
244+
$errorMessage = $config->requirements["error_message"];
245+
} else {
246+
$errorMessage = $this->violationFormatter->formatList($config, $errors);
247+
}
248+
throw new BadRequestHttpException($errorMessage);
241249
}
242-
243250
return null === $default ? '' : $default;
244251
}
245252

Tests/Request/ParamFetcherTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,47 @@ public function testNullValidatorWithoutRequirements()
709709
$queryFetcher->setController($this->controller);
710710
$this->assertEquals('foobar', $queryFetcher->get('bizoo'));
711711
}
712+
713+
public function testCustomErrorMessage()
714+
{
715+
$param = new QueryParam();
716+
$param->name = 'fero';
717+
$errorMessage = "variable must be an integer";
718+
$param->requirements = array('rule' => '\d+', 'error_message' => $errorMessage);
719+
$param->description = 'integer value';
720+
$param->strict = true;
721+
722+
$request = new Request(array('fero' => 'foobar'), array(), array('_controller' => __CLASS__.'::stubAction'));
723+
$reader = $this->getMockBuilder('FOS\RestBundle\Request\ParamReader')
724+
->disableOriginalConstructor()
725+
->getMock();
726+
727+
$reader->expects($this->any())
728+
->method('read')
729+
->will($this->returnValue(array('fero' => $param)));
730+
731+
$constraint = new Regex(array(
732+
'pattern' => '#^\d+$#xsu',
733+
'message' => $errorMessage,
734+
));
735+
736+
$errors = new ConstraintViolationList(array(
737+
new ConstraintViolation($errorMessage, null, array(), null, null, null),
738+
));
739+
740+
$this->validator->expects($this->once())
741+
->method('validateValue')
742+
->with('foobar', $constraint)
743+
->will($this->returnValue($errors));
744+
745+
$queryFetcher = new ParamFetcher($reader, $request, $this->violationFormatter, $this->validator);
746+
$queryFetcher->setController($this->controller);
747+
748+
try {
749+
$queryFetcher->get('fero');
750+
$this->fail('Fetching get() in strict mode with no default value did not throw an exception');
751+
} catch (HttpException $httpException) {
752+
$this->assertEquals($errorMessage, $httpException->getMessage());
753+
}
754+
}
712755
}

0 commit comments

Comments
 (0)