87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Http\Requests;
|
||
|
||
use App\Models\PreregistrationPage;
|
||
use Illuminate\Foundation\Http\FormRequest;
|
||
use Illuminate\Validation\Rules\Email;
|
||
|
||
class SubscribePublicPageRequest extends FormRequest
|
||
{
|
||
public function authorize(): bool
|
||
{
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function rules(): array
|
||
{
|
||
/** @var PreregistrationPage $page */
|
||
$page = $this->route('publicPage');
|
||
|
||
$emailRule = (new Email)
|
||
->rfcCompliant()
|
||
->preventSpoofing();
|
||
|
||
return [
|
||
'first_name' => ['required', 'string', 'max:255'],
|
||
'last_name' => ['required', 'string', 'max:255'],
|
||
'email' => ['required', 'string', 'max:255', $emailRule],
|
||
'phone' => $page->phone_enabled
|
||
? ['nullable', 'string', 'regex:/^[0-9]{8,15}$/']
|
||
: ['nullable', 'string', 'max:255'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return array<string, string>
|
||
*/
|
||
public function messages(): array
|
||
{
|
||
return [
|
||
'email' => __('Please enter a valid email address.'),
|
||
'phone.regex' => __('Please enter a valid phone number (8–15 digits).'),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return array<string, string>
|
||
*/
|
||
public function attributes(): array
|
||
{
|
||
return [
|
||
'first_name' => __('First name'),
|
||
'last_name' => __('Last name'),
|
||
'email' => __('Email'),
|
||
'phone' => __('Phone'),
|
||
];
|
||
}
|
||
|
||
protected function prepareForValidation(): void
|
||
{
|
||
$email = $this->input('email');
|
||
if (is_string($email)) {
|
||
$this->merge([
|
||
'email' => strtolower(trim($email)),
|
||
]);
|
||
}
|
||
|
||
/** @var PreregistrationPage $page */
|
||
$page = $this->route('publicPage');
|
||
$phone = $this->input('phone');
|
||
if (! $page->phone_enabled) {
|
||
$this->merge(['phone' => null]);
|
||
|
||
return;
|
||
}
|
||
if (is_string($phone)) {
|
||
$digits = preg_replace('/\D+/', '', $phone);
|
||
$this->merge(['phone' => $digits === '' ? null : $digits]);
|
||
}
|
||
}
|
||
}
|