|string> */ public function rules(): array { return [ 'email' => ['required', 'string', 'email:rfc', 'max:254'], 'first_name' => ['nullable', 'string', 'max:100'], 'last_name' => ['nullable', 'string', 'max:100'], ]; } /** * @return array|string> */ public function rulesForStore(): array { return $this->rulesForRequest(unique: true); } /** * @return array|string> */ public function rulesForUpdate(Contact $contact): array { return $this->rulesForRequest(unique: true, ignore: $contact); } /** * @return array */ public function messages(): array { return [ 'first_name.max' => 'First name may not be greater than 100 characters.', 'last_name.max' => 'Last name may not be greater than 100 characters.', 'email.required' => 'Email is required.', 'email.email' => 'Email must be a valid email address.', 'email.max' => 'Email may not be greater than 254 characters.', 'email.unique' => 'Email has already been taken.', ]; } /** * @return array|string> */ private function rulesForRequest(bool $unique, ?Contact $ignore = null): array { $rules = $this->rules(); /** * @var array $emailRules */ $emailRules = Arr::pull($rules, 'email'); if ($unique) { $rule = Rule::unique('contacts', 'email'); if ($ignore !== null) { $rule = $rule->ignore($ignore->uuid->toString()); } $emailRules[] = $rule; } /** @var array|string> $result */ $result = [ ...$rules, 'email' => $emailRules, ]; return $result; } }