CreateOrUpdateRequest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Project;
  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 CreateOrUpdateRequest 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. return [
  29. 'title' => 'required|max:255',
  30. 'status' => [
  31. 'required',
  32. new Enum(RequirementStatus::class),
  33. ],
  34. 'asset_id' => [
  35. 'nullable',
  36. Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
  37. ],
  38. 'mailto' => [
  39. 'array',
  40. function ($attribute, $value, $fail) {
  41. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  42. if ($userCount != count($value)) {
  43. $fail('The selected user is invalid.');
  44. }
  45. }
  46. ],
  47. 'reviewed_by' => [
  48. 'nullable',
  49. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  50. ],
  51. 'priority' => 'required|in:1,2,3,4',
  52. 'requirement_group_id' => [
  53. 'nullable',
  54. 'sometimes',
  55. Rule::exists('requirement_groups', 'id')->where($this->userCompanyWhere()),
  56. ],
  57. 'project_id' => [
  58. 'nullable',
  59. 'array',
  60. function ($attribute, $value, $fail) {
  61. $count = Project::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  62. if ($count != count($value)) {
  63. $fail('The selected project is invalid.');
  64. }
  65. }
  66. ],
  67. 'plan_id' => [
  68. 'nullable',
  69. 'sometimes',
  70. Rule::exists('plans', 'id')->where($this->userCompanyWhere()),
  71. ]
  72. ];
  73. }
  74. }