123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Http\Requests\API\Approval;
- use App\Models\Enums\ApprovalObjectType;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Validation\Rules\Enum;
- class CreateOrUpdateRequest extends FormRequest
- {
- /**
- * Determine if the user is authorized to make this request.
- */
- public function authorize(): bool
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- *
- * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
- */
- public function rules(): array
- {
- $rule = [];
- if ($this->method() == "POST") {
- $rule = [
- ...$rule,
- 'object_type' => [
- 'required',
- new Enum(ApprovalObjectType::class),
- ],
- 'object_id' => [
- 'required',
- function ($attribute, $value, $fail) {
- $exist = ApprovalObjectType::from($this->get("object_type"))
- ->modelBuilderAllowed($value)
- ->where("company_id", Auth::user()->company_id)
- ->where('id', $value)
- ->count();
- if (! $exist) {
- $fail('Resources without permission to access.');
- }
- }
- ]
- ];
- }
- return $rule;
- }
- }
|