UserRequest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class UserRequest extends FormRequest
  5. {
  6. /**
  7. * Determine if the user is authorized to make this request.
  8. *
  9. * @return bool
  10. */
  11. public function authorize()
  12. {
  13. return true;
  14. }
  15. /**
  16. * Get the validation rules that apply to the request.
  17. *
  18. * @return array
  19. */
  20. public function rules()
  21. {
  22. switch ($this->getMethod()) {
  23. case "PUT":
  24. return [
  25. 'sex' => 'in:1,2',
  26. 'feed_sex' => 'in:1,2',
  27. 'nickname' => 'between:1,12',
  28. 'height' => 'integer|between:150,200',
  29. 'introduce' => 'max:180',
  30. 'expect' => 'max:180',
  31. 'qq' => ['integer', 'regex:/^[0-9_]{5,10}$/'],
  32. 'weixin' => ['regex:/^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$/'],
  33. 'school' => 'string',
  34. 'age' => ["before_or_equal:-18year", "after_or_equal:-100year"]
  35. ];
  36. break;
  37. default:
  38. return [];
  39. }
  40. }
  41. public function messages()
  42. {
  43. return [
  44. 'sex.*' => '性别只能选取[男/女]',
  45. 'feed_sex.*' => '性取向只能选取[男/女]',
  46. 'weixin.*' => '请输入正确的微信号',
  47. 'qq.*' => '请输入正确的QQ号',
  48. 'school.*' => '请输入正确的学校全称',
  49. 'age.before_or_equal' => '生日 必须18周岁以上',
  50. 'age.after_or_equal' => '生日 必须100周岁以下',
  51. ];
  52. }
  53. public function attributes()
  54. {
  55. return [
  56. 'sex' => '性别',
  57. 'feed_sex' => '性取向',
  58. 'nickname' => '昵称',
  59. 'introduce' => '个性签名',
  60. 'expect' => '期望',
  61. 'height' => '身高',
  62. 'age' => '生日',
  63. 'weixin' => '微信号',
  64. 'qq' => 'QQ号',
  65. ];
  66. }
  67. }