CourseRequest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Http\Requests;
  3. use Request;
  4. use Carbon\Carbon;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. class CourseRequest extends FormRequest
  7. {
  8. /**
  9. * Determine if the user is authorized to make this request.
  10. *
  11. * @return bool
  12. */
  13. public function authorize()
  14. {
  15. return true;
  16. }
  17. /**
  18. * Get the validation rules that apply to the request.
  19. *
  20. * @return array
  21. */
  22. public function rules()
  23. {
  24. // 持续性课程,开始结束日期为空
  25. if (!Request::input('start_date') && !Request::input('end_date')) {
  26. $if_sDate = 'nullable';
  27. $if_eDate = 'nullable';
  28. }
  29. else {
  30. if (Request::input('start_date')) {
  31. // 开始日期必须大于当前日期
  32. $if_sDate = 'required|after:'.Carbon::now();
  33. // 结束日期必须大于开始日期
  34. $start_date = date_create(Request::input('start_date'));
  35. $start_date = date_format($start_date, 'Y/m/d');
  36. $if_eDate = 'required|after:'.$start_date;
  37. }
  38. else {
  39. $if_sDate = 'required';
  40. $if_eDate = 'required';
  41. }
  42. }
  43. return [
  44. 'name' => 'required|string|max:50',
  45. 'start_date' => $if_sDate,
  46. 'end_date' => $if_eDate,
  47. 'keywords' => 'required|string|max:255',
  48. 'description' => 'required|string|max:255',
  49. 'belong' => 'required|max:50',
  50. 'text' => 'required'
  51. ];
  52. }
  53. public function messages()
  54. {
  55. return [
  56. 'start_date.after' => '必须大于当前日期',
  57. 'end_date.after' => '必须大于开始日期',
  58. 'start_date.required' => '开始日期和结束日期必须同时填写或不填',
  59. 'end_date.required' => '开始日期和结束日期必须同时填写或不填'
  60. ];
  61. }
  62. }