user() !== null; } /** * Prepare the data for validation. */ protected function prepareForValidation(): void { $acceptanceCriteria = $this->input('acceptance_criteria'); if ($acceptanceCriteria !== null && ! is_array($acceptanceCriteria)) { return; } $this->merge([ 'acceptance_criteria' => AcceptanceCriteriaChecklistMapper::normalize( $acceptanceCriteria, ), ]); } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { /** @var Project $project */ $project = $this->route('project'); return [ 'acceptance_criteria' => ['nullable', 'array'], 'acceptance_criteria.*' => ['array'], 'acceptance_criteria.*.id' => ['nullable', 'integer'], 'acceptance_criteria.*.text' => ['required', 'string'], 'acceptance_criteria.*.checked' => ['required', 'boolean'], 'acceptance_criteria.*.indent' => ['required', 'integer', 'min:0', 'max:'.AcceptanceCriteriaChecklistMapper::MAX_INDENT_LEVEL], 'assignee_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')], 'description' => ['nullable', 'string'], 'parent_ticket_id' => [ 'nullable', 'integer', Rule::exists('tickets', 'id')->where('project_id', $project->id), ], 'ticket_priority_id' => [ 'required', 'integer', Rule::exists('ticket_priorities', 'id')->where('project_id', $project->id), ], 'ticket_status_id' => [ 'required', 'integer', Rule::exists('ticket_statuses', 'id')->where('project_id', $project->id), ], 'ticket_type_id' => [ 'required', 'integer', Rule::exists('ticket_types', 'id')->where('project_id', $project->id), ], 'story_points' => ['nullable', 'integer', 'min:0'], 'title' => ['required', 'string', 'max:255'], ]; } /** * @return array */ public function after(): array { return [ function (Validator $validator): void { /** @var array $validated */ $validated = $this->safe()->all(); /** @var Project $project */ $project = $this->route('project'); $parentTicketId = Arr::get($validated, 'parent_ticket_id'); if ($parentTicketId === null) { return; } $parentTicket = Ticket::query() ->where('project_id', $project->id) ->with('ticketType') ->find($parentTicketId); if ($parentTicket !== null && ! $parentTicket->ticketType?->is_parent) { $validator->errors()->add('parent_ticket_id', 'Only tickets with a parent-enabled type can be selected as a parent.'); } }, ]; } }