Request.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/10 0010
  6. * Time: 13:07
  7. */
  8. namespace App\Http\Requests;
  9. use Illuminate\Foundation\Http\FormRequest;
  10. use Illuminate\Contracts\Validation\Validator;
  11. use Illuminate\Validation\ValidationException;
  12. use Symfony\Component\HttpFoundation\Response;
  13. class Request extends FormRequest
  14. {
  15. protected function failedValidation(Validator $validator)
  16. {
  17. $messageList = [];
  18. foreach ($validator->errors()->messages() as $items) {
  19. foreach ($items as $item) {
  20. $messageList[] = $item;
  21. }
  22. }
  23. $message = implode('<br>', $messageList);
  24. $format = json_encode(['message' => $message]);
  25. throw (new ValidationException($validator, new Response($format, 422)))
  26. ->errorBag($this->errorBag)
  27. ->redirectTo($this->getRedirectUrl());
  28. }
  29. // public function messages()
  30. // {
  31. // return [
  32. // 'required' => ':attribute不能为空',
  33. // 'in' => ':attribute必须是以下取值: :values',
  34. // 'numeric' => ':attribute必须是数字',
  35. // 'max' => [
  36. // 'numeric' => ':attribute 应小于:max ',
  37. // 'file' => ':attribute 应小于:max kb',
  38. // 'string' => ':attribute应小于 :min 位',
  39. // 'array' => ':attribute应大于 :min 列表',
  40. // ],
  41. // 'min' => [
  42. // 'numeric' => ':attribute应大于 :min ',
  43. // 'file' => ':attribute应大于 :min kb',
  44. // 'string' => ':attribute应大于 :min 位',
  45. // 'array' => ':attribute应大于 :min 列表',
  46. // ],
  47. // 'unique' => ':attribute 已存在',
  48. // 'array' => ':attribute应为列表'
  49. // ];
  50. // }
  51. }