File tree Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Fesor \RequestObject ;
4
4
5
+ use Symfony \Component \HttpFoundation \JsonResponse ;
5
6
use Symfony \Component \Validator \Constraint ;
7
+ use Symfony \Component \Validator \ConstraintViolation ;
8
+ use Symfony \Component \Validator \ConstraintViolationListInterface ;
6
9
7
- class RequestObject
10
+ class RequestObject implements ErrorResponseProvider
8
11
{
9
12
private $ payload ;
10
13
@@ -56,4 +59,20 @@ public function all()
56
59
{
57
60
return $ this ->payload ;
58
61
}
62
+
63
+ /**
64
+ * @param ConstraintViolationListInterface $errors
65
+ *
66
+ * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
67
+ */
68
+ public function getErrorResponse (ConstraintViolationListInterface $ errors )
69
+ {
70
+ return new JsonResponse ([
71
+ 'errors ' => array_map (function (ConstraintViolation $ violation ) {
72
+ return [
73
+ $ violation ->getPropertyPath () => $ violation ->getMessage (),
74
+ ];
75
+ }, iterator_to_array ($ errors ))
76
+ ], 422 );
77
+ }
59
78
}
You can’t perform that action at this time.
0 commit comments