-
app/Rules/MaxYearsExperienceRule.php
Open in GitHubuse App\Types\Rule; use App\Models\Tool; class MaxYearsExperienceRule extends Rule { public function message() : string { return 'The tool has not existed for that long.'; } public function passes($attribute, $value) : bool { $max = Tool::query() ->where('id', $this->parameters[0]) ->value('originated'); $age = (now()->year - $max) === 0 ? 1 : now()->year - $max; return $value <= $age; } }
-
app/Requests/Requirement/StoreRequest.php
Open in GitHubuse App\Types\FormRequest; use Illuminate\Validation\Rule; use App\Rules\MaxYearsExperienceRule; class StoreRequest extends FormRequest { public function authorize() : bool { $payload = [ Requirement::class, organization(), $this->route('vacancy'), ]; return user()->can('store', $payload); } public function rules() : array { $unique = Rule::unique('requirements') ->where('vacancy_id', $this->route('vacancy')->id); $years = MaxYearsExperienceRule::make($this->tool_id); return [ 'tool_id' => ['bail', 'required', 'uuid', 'exists:tools,id', $unique], 'years' => ['bail', 'required', 'integer', 'min:1', $years], 'optional' => 'bail|required|boolean', 'summary' => 'bail|nullable|string|max:200', ]; } }