Files
preregister/app/Http/Requests/SubscribePublicPageRequest.php
bert.hausmans 17e784fee7 feat: E.164 phone validation and storage with libphonenumber
- Add giggsey/libphonenumber-for-php, PhoneNumberNormalizer, ValidPhoneNumber rule

- Store subscribers as E.164; mutator normalizes on save; optional phone required from form block

- Migration to normalize legacy subscriber phones; Mailwizz/search/UI/tests updated

- Add run-deploy-from-local.sh and PREREGISTER_DEFAULT_PHONE_REGION in .env.example

Made-with: Cursor
2026-04-04 14:25:52 +02:00

107 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests;
use App\Models\PreregistrationPage;
use App\Rules\ValidPhoneNumber;
use App\Services\PhoneNumberNormalizer;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
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');
$page->loadMissing('blocks');
$emailRule = (new Email)
->rfcCompliant()
->preventSpoofing();
$phoneRules = ['nullable', 'string', 'max:255'];
if ($page->isPhoneFieldEnabledForSubscribers()) {
$phoneRules = [
Rule::requiredIf(fn (): bool => $page->isPhoneFieldRequiredForSubscribers()),
'nullable',
'string',
'max:32',
new ValidPhoneNumber(app(PhoneNumberNormalizer::class)),
];
}
return [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'max:255', $emailRule],
'phone' => $phoneRules,
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'email' => __('Please enter a valid email address.'),
];
}
/**
* @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');
if (! $page->isPhoneFieldEnabledForSubscribers()) {
$this->merge(['phone' => null]);
return;
}
$phone = $this->input('phone');
if ($phone === null || $phone === '') {
$this->merge(['phone' => null]);
return;
}
if (is_string($phone)) {
$trimmed = trim($phone);
$this->merge(['phone' => $trimmed === '' ? null : $trimmed]);
}
}
}