BatchCreateRequest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\RequirementGroup;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Http\FormRequest;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\Rules\Enum;
  11. class BatchCreateRequest extends FormRequest
  12. {
  13. use RuleHelper;
  14. /**
  15. * Determine if the user is authorized to make this request.
  16. */
  17. public function authorize(): bool
  18. {
  19. return true;
  20. }
  21. /**
  22. * Get the validation rules that apply to the request.
  23. *
  24. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  25. */
  26. public function rules(): array
  27. {
  28. $requirementGroupIds = RequirementGroup::where('company_id',Auth::user()->company_id)->pluck('id')->toArray();
  29. $IdsAndDitto=$requirementGroupIds;
  30. $IdsAndDitto[] = 'ditto';
  31. return [
  32. '*.title' => 'required|max:255',
  33. '*.asset_id' => [
  34. 'required',
  35. Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
  36. ],
  37. '*.priority' => 'required|in:1,2,3,4,ditto',
  38. '0.priority' => 'required|in:1,2,3,4', //第一个元素不能为ditto
  39. '*.requirement_group_id' => [
  40. 'nullable',
  41. 'sometimes',
  42. Rule::in($IdsAndDitto),
  43. ],
  44. '0.requirement_group_id' => [ // 第一个元素不能为ditto
  45. 'nullable',
  46. 'sometimes',
  47. Rule::in($requirementGroupIds),
  48. ],
  49. ];
  50. }
  51. }