CreateOrUpdateRequest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\User;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Validation\Rule;
  9. use Illuminate\Validation\Rules\Enum;
  10. class CreateOrUpdateRequest extends FormRequest
  11. {
  12. use RuleHelper;
  13. /**
  14. * Determine if the user is authorized to make this request.
  15. */
  16. public function authorize(): bool
  17. {
  18. return true;
  19. }
  20. /**
  21. * Get the validation rules that apply to the request.
  22. *
  23. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  24. */
  25. public function rules(): array
  26. {
  27. return [
  28. 'title' => 'required|max:255',
  29. 'status' => [
  30. 'required',
  31. new Enum(RequirementStatus::class),
  32. ],
  33. 'asset_id' => [
  34. 'required',
  35. Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
  36. ],
  37. 'mailto' => [
  38. 'array',
  39. function ($attribute, $value, $fail) {
  40. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  41. if ($userCount != count($value)) {
  42. $fail('The selected user is invalid.');
  43. }
  44. }
  45. ],
  46. 'reviewed_by' => [
  47. 'nullable',
  48. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  49. ],
  50. 'priority' => 'required|in:1,2,3,4',
  51. 'requirement_group_id' => [
  52. 'nullable',
  53. 'sometimes',
  54. Rule::exists('requirement_groups', 'id')->where($this->userCompanyWhere()),
  55. ]
  56. ];
  57. }
  58. }