CreateOrUpdateRequest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Requests\API\Approval;
  3. use App\Models\Enums\ApprovalObjectType;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Validation\Rules\Enum;
  7. class CreateOrUpdateRequest extends FormRequest
  8. {
  9. /**
  10. * Determine if the user is authorized to make this request.
  11. */
  12. public function authorize(): bool
  13. {
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  20. */
  21. public function rules(): array
  22. {
  23. $rule = [];
  24. if ($this->method() == "POST") {
  25. $rule = [
  26. ...$rule,
  27. 'object_type' => [
  28. 'required',
  29. new Enum(ApprovalObjectType::class),
  30. ],
  31. 'object_id' => [
  32. 'required',
  33. function ($attribute, $value, $fail) {
  34. $exist = ApprovalObjectType::from($this->get("object_type"))
  35. ->modelBuilderAllowed($value)
  36. ->where("company_id", Auth::user()->company_id)
  37. ->where('id', $value)
  38. ->count();
  39. if (! $exist) {
  40. $fail('Resources without permission to access.');
  41. }
  42. }
  43. ]
  44. ];
  45. }
  46. return $rule;
  47. }
  48. }