CreateOrUpdateRequest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. 'priority' => 'required|in:1,2,3,4',
  47. 'requirement_group_id' => [
  48. 'required',
  49. Rule::exists('requirement_groups', 'id')->where($this->userCompanyWhere()),
  50. ]
  51. ];
  52. }
  53. }