@@ -248,23 +248,7 @@ public function postRegisterAction()
248
248
$ errors = $ validator ->validate ($ user );
249
249
250
250
if (count ($ errors ) > 0 ) {
251
- $ errorArray = [];
252
- foreach ($ errors as $ error ) {
253
- $ constraint = $ error ->getConstraint ();
254
- $ errorItem = array (
255
- "error_description " => $ error ->getPropertyPath ().': ' .$ error ->getMessage ().' ' .$ error ->getInvalidValue (),
256
- "show_message " => $ this ->get ('translator ' )->trans ($ constraint ->payload ['api_error ' ], array (), 'messages ' , $ request ->getLocale ())
257
- );
258
- array_push ($ errorArray , $ errorItem );
259
- $ this ->logMessage (400 , $ errorItem ['error_description ' ] );
260
- }
261
- return new JsonResponse (array (
262
- "code " => 400 ,
263
- "error " => "Bad Request " ,
264
- "error_description " => $ errorArray [0 ]['error_description ' ],
265
- "show_message " => $ errorArray [0 ]['show_message ' ],
266
- 'errors ' => $ errorArray
267
- ));
251
+ return $ this ->reportValidationErrors ($ request , $ errors );
268
252
}
269
253
270
254
// Everything ok, now write the user record
@@ -457,24 +441,8 @@ public function editProfileAction()
457
441
$ errors = $ validator ->validate ($ user , null , array ('profile_edit ' ));
458
442
459
443
if (count ($ errors ) > 0 ) {
460
- $ errorArray = [];
461
- foreach ($ errors as $ error ) {
462
- $ constraint = $ error ->getConstraint ();
463
- $ errorItem = array (
464
- "error_description " => $ error ->getPropertyPath ().': ' .$ error ->getMessage ().' ' .$ error ->getInvalidValue (),
465
- "show_message " => $ this ->get ('translator ' )->trans ($ constraint ->payload ['api_error ' ], array (), 'messages ' , $ request ->getLocale ())
466
- );
467
- array_push ($ errorArray , $ errorItem );
468
- $ this ->logMessage (400 , $ errorItem ['error_description ' ] );
469
- }
470
- return new JsonResponse (array (
471
- "code " => 400 ,
472
- "error " => "Bad Request " ,
473
- "error_description " => $ errorArray [0 ]['error_description ' ],
474
- "show_message " => $ errorArray [0 ]['show_message ' ],
475
- 'errors ' => $ errorArray
476
- ));
477
- }
444
+ return $ this ->reportValidationErrors ($ request , $ errors );
445
+ }
478
446
479
447
// Everything ok, now update the user record
480
448
$ userManager = $ this ->get ('fos_user.user_manager ' );
@@ -697,6 +665,27 @@ private function fetchAccessToken(Request $request, $grantType)
697
665
return $ response ['result ' ];
698
666
}
699
667
668
+ private function reportValidationErrors (Request $ request , \Symfony \Component \Validator \ConstraintViolationList $ errors )
669
+ {
670
+ $ errorArray = [];
671
+ foreach ($ errors as $ error ) {
672
+ $ constraint = $ error ->getConstraint ();
673
+ $ errorItem = array (
674
+ "error_description " => $ error ->getPropertyPath ().': ' .$ error ->getMessage ().' ' .$ error ->getInvalidValue (),
675
+ "show_message " => $ this ->get ('translator ' )->trans ($ constraint ->payload ['api_error ' ], array (), 'messages ' , $ request ->getLocale ())
676
+ );
677
+ array_push ($ errorArray , $ errorItem );
678
+ $ this ->logMessage (400 , $ errorItem ['error_description ' ] );
679
+ }
680
+ return new JsonResponse (array (
681
+ "code " => 400 ,
682
+ "error " => "Bad Request " ,
683
+ "error_description " => $ errorArray [0 ]['error_description ' ],
684
+ "show_message " => $ errorArray [0 ]['show_message ' ],
685
+ 'errors ' => $ errorArray
686
+ ));
687
+ }
688
+
700
689
private function logAndThrowError ($ errCode = 400 , $ errMsg = 'Bad Request ' , $ showMsg = '' , $ locale = 'en ' ) {
701
690
$ this ->logMessage ($ errCode , $ errMsg );
702
691
throw new HttpException ($ errCode , $ errMsg .($ showMsg ? '#showme# ' .$ showMsg : '' ) );
0 commit comments