Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // < Laravel 5.5 use formatErrors
- $errors = $validator->messages()->toArray();
- foreach ($errors as $error => $message) {
- // Check if error should be an array
- if (strpos($error, '.')) {
- // Ex: {parent}.0
- $parent = explode('.', $error)[0];
- // Ex: items.{nth}
- $nth = explode('.', $error)[1];
- // Delete the string-key array error
- // Ex: 'phones.0'
- unset($errors[$error]);
- $errors[$parent][$nth] = $message[0];
- } else {
- $errors[$error] = $message[0];
- }
- }
- // This sorts array errors
- // to make sure the return response
- // is an array, not a number-based object.
- // Ex: { '1': 'error', '0': {} }
- // This just ensures consistency because
- // it can mess up the front-end
- foreach($errors as $key => $message) {
- if (is_array($message)) {
- ksort($errors[$key]);
- }
- }
Add Comment
Please, Sign In to add comment