merge([ 'name' => $this->trimValue($this->name), 'email' => $this->email ? Str::lower(trim($this->email)) : null, 'phone' => $this->phone ? preg_replace('/[^0-9+\-\s]/', '', $this->phone) : null, 'company' => $this->trimValue($this->company), 'country_region' => $this->trimValue($this->country_region), 'industry' => $this->trimValue($this->industry), 'topic' => $this->trimValue($this->topic), 'message' => $this->trimValue($this->message), ]); } /** * Trim a value if it's a string. */ private function trimValue($value): ?string { return is_string($value) ? trim($value) : $value; } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { return [ 'name' => ['required', 'string', 'max:100'], 'email' => ['required', 'email', 'max:255'], 'phone' => ['nullable', 'string', 'max:50'], 'company' => ['nullable', 'string', 'max:100'], 'country_region' => ['nullable', 'string', 'max:100'], 'industry' => ['nullable', 'string', 'max:100'], 'topic' => ['nullable', 'string', 'max:100'], 'message' => ['required', 'string', 'max:2000'], 'form_source' => ['nullable', 'string', 'in:home,contact-us'], // Honeypot field - must be empty 'trap_field' => ['present', 'max:0'], // Minimum fill time validation (3 seconds) 'form_started_at' => ['required', 'integer'], ]; } /** * Get custom messages for validator errors. * * @return array */ public function messages(): array { return [ 'name.required' => 'Please enter your name.', 'name.max' => 'Name cannot exceed 100 characters.', 'email.required' => 'Please enter your email address.', 'email.email' => 'Please enter a valid email address.', 'message.required' => 'Please enter your message.', 'message.max' => 'Message cannot exceed 2000 characters.', 'trap_field.max' => 'Invalid submission.', 'form_started_at.required' => 'Invalid submission.', ]; } /** * Configure the validator instance. * * @param \Illuminate\Validation\Validator $validator * @return void */ public function withValidator($validator): void { $validator->after(function ($validator) { // Check minimum fill time (3 seconds) if ($this->form_started_at) { $elapsedSeconds = time() - (int)$this->form_started_at; if ($elapsedSeconds < 3) { $validator->errors()->add('form_started_at', 'Please take your time to fill out the form.'); } } }); } /** * Get validated data with meta information. * * @return array */ public function validatedWithMeta(): array { $validated = $this->safe()->only([ 'name', 'email', 'phone', 'company', 'country_region', 'industry', 'topic', 'message', ]); $meta = [ 'ip' => $this->ip(), 'user_agent' => $this->userAgent(), 'referer' => $this->header('referer'), 'page_url' => $this->header('referer'), 'form_source' => $this->input('form_source', 'contact-us'), ]; // Add UTM parameters if present $utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; $utm = []; foreach ($utmParams as $param) { if ($this->has($param)) { $utm[$param] = $this->input($param); } } if (!empty($utm)) { $meta['utm'] = $utm; } return [ 'data' => $validated, 'meta' => $meta, 'type' => 'general', ]; } }