Guest User

Untitled

a guest
Oct 18th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // < Laravel 5.5 use formatErrors
  2.  
  3. $errors = $validator->messages()->toArray();
  4.  
  5. foreach ($errors as $error => $message) {
  6. // Check if error should be an array
  7. if (strpos($error, '.')) {
  8. // Ex: {parent}.0
  9. $parent = explode('.', $error)[0];
  10.  
  11. // Ex: items.{nth}
  12. $nth = explode('.', $error)[1];
  13.  
  14. // Delete the string-key array error
  15. // Ex: 'phones.0'
  16. unset($errors[$error]);
  17. $errors[$parent][$nth] = $message[0];
  18. } else {
  19. $errors[$error] = $message[0];
  20. }
  21. }
  22.  
  23. // This sorts array errors
  24. // to make sure the return response
  25. // is an array, not a number-based object.
  26. // Ex: { '1': 'error', '0': {} }
  27. // This just ensures consistency because
  28. // it can mess up the front-end
  29. foreach($errors as $key => $message) {
  30. if (is_array($message)) {
  31. ksort($errors[$key]);
  32. }
  33. }
Add Comment
Please, Sign In to add comment