BatchCreateRequest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Requests\API\Requirement;
  3. use App\Http\Requests\RuleHelper;
  4. use App\Models\Enums\RequirementStatus;
  5. use App\Models\Plan;
  6. use App\Models\Project;
  7. use App\Models\RequirementGroup;
  8. use App\Models\User;
  9. use Illuminate\Foundation\Http\FormRequest;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Validation\Rule;
  12. use Illuminate\Validation\Rules\Enum;
  13. class BatchCreateRequest extends FormRequest
  14. {
  15. use RuleHelper;
  16. /**
  17. * Determine if the user is authorized to make this request.
  18. */
  19. public function authorize(): bool
  20. {
  21. return true;
  22. }
  23. /**
  24. * Get the validation rules that apply to the request.
  25. *
  26. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  27. */
  28. public function rules(): array
  29. {
  30. $requirementGroupIds = RequirementGroup::where('company_id',Auth::user()->company_id)->pluck('id')->toArray();
  31. $IdsAndDitto=$requirementGroupIds;
  32. $IdsAndDitto[] = 'ditto';
  33. $planIds = Plan::where('company_id',Auth::user()->company_id)->pluck('id')->toArray();
  34. $planIdsAndDitto=$planIds;
  35. $planIdsAndDitto[] = 'ditto';
  36. return [
  37. '*.title' => 'required|max:255',
  38. '*.asset_id' => [
  39. 'nullable',
  40. 'sometimes',
  41. Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
  42. ],
  43. '*.priority' => 'required|in:1,2,3,4,ditto',
  44. '0.priority' => 'required|in:1,2,3,4', //第一个元素不能为ditto
  45. '*.requirement_group_id' => [
  46. 'nullable',
  47. 'sometimes',
  48. Rule::in($IdsAndDitto),
  49. ],
  50. '0.requirement_group_id' => [ // 第一个元素不能为ditto
  51. 'nullable',
  52. 'sometimes',
  53. Rule::in($requirementGroupIds),
  54. ],
  55. '*.plan_id' => [
  56. 'nullable',
  57. 'sometimes',
  58. Rule::in($planIdsAndDitto),
  59. ],
  60. '0.plan_id' => [ // 第一个元素不能为ditto
  61. 'nullable',
  62. 'sometimes',
  63. Rule::in($planIdsAndDitto),
  64. ],
  65. '*.project_id' => [
  66. 'nullable',
  67. 'array',
  68. function ($attribute, $value, $fail) {
  69. $count = Project::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  70. if ($count != count($value)) {
  71. $fail('The selected project is invalid.');
  72. }
  73. }
  74. ],
  75. '0.project_id' => [// 第一个元素不能为ditto
  76. 'nullable',
  77. 'array',
  78. function ($attribute, $value, $fail) {
  79. $count = Project::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  80. if ($count != count($value)) {
  81. $fail('The selected project is invalid.');
  82. }
  83. }
  84. ],
  85. ];
  86. }
  87. }