ldc-tregalette/app/Http/Requests/Fo/Customers/CreateAccountRequest.php

123 lines
4.5 KiB
PHP
Raw Normal View History

2023-12-15 15:53:55 +00:00
<?php
namespace App\Http\Requests\Fo\Customers;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
2023-12-18 10:30:19 +01:00
use Illuminate\Validation\Rule;
2023-12-15 15:53:55 +00:00
use Illuminate\Validation\Rules\Password;
2023-12-18 10:30:19 +01:00
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
2023-12-15 15:53:55 +00:00
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Models\Customer as CrmCustomer;
class CreateAccountRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
2024-01-26 16:24:14 +01:00
public function authorize(): bool
2023-12-15 15:53:55 +00:00
{
return Auth::guard('frontend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
2024-01-26 16:24:14 +01:00
public function prepareForValidation(): void
2023-12-15 15:53:55 +00:00
{
$this->merge([
2024-01-26 16:24:14 +01:00
'phone' => $this->input('phone'),
2023-12-18 10:30:19 +01:00
'opt_in' => $this->boolean('opt_in'),
'ok_cgv' => $this->boolean('ok_cgv')
2023-12-15 15:53:55 +00:00
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
2024-01-26 16:24:14 +01:00
public function rules(): array
2023-12-15 15:53:55 +00:00
{
return [
2024-01-26 16:24:14 +01:00
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'phone' => 'nullable|max:255|phone:FR',
'address' => 'required|string|min:1|max:255',
'address2' => 'nullable|string|min:1|max:255',
2023-12-18 10:30:19 +01:00
'postal_code' => 'required|string|min:1|postal_code:FR',
2024-01-26 16:24:14 +01:00
'city' => 'required|string|min:2|max:255',
'birth_date' => 'required|date',
'country' => [
2023-12-18 10:30:19 +01:00
'required', new EnumIsValidRule(Country::class),
Rule::in(Country::getValidCountries())
],
2024-01-26 16:24:14 +01:00
'email' => [
2023-12-15 15:53:55 +00:00
'required', 'email:rfc,dns,spoof', 'max:255',
// * Does email already exists in CRM ?
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function (string $attribute, $value, \Closure $fail) {
if (CrmCustomer::findWithEmail(\strval($value))) {
$fail('Un compte existe déjà avec cette adresse.');
}
},
'unique:customers,email',
],
2024-01-26 16:24:14 +01:00
'password' => [
2023-12-18 10:30:19 +01:00
'required', 'confirmed', Password::min(8)->mixedCase()
->numbers()->symbols()->uncompromised()
2023-12-15 15:53:55 +00:00
],
2024-01-26 16:24:14 +01:00
'ok_cgv' => 'accepted',
'opt_in' => 'required|boolean',
2023-12-15 15:53:55 +00:00
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
2024-01-26 16:24:14 +01:00
public function attributes(): array
2023-12-15 15:53:55 +00:00
{
return [
2024-01-26 16:24:14 +01:00
'lastname' => trans('validation.attributes.first_name'),
'firstname' => trans('validation.attributes.first_name'),
'phone' => trans('validation.attributes.phone'),
'address' => trans('validation.attributes.line_address_1'),
'address2' => trans('validation.attributes.line_address_2'),
'country' => trans('validation.attributes.country'),
'postal_code' => trans('validation.attributes.postal_code'),
'city' => trans('validation.attributes.city'),
'birth_date' => trans('validation.attributes.date_of_birth'),
'email' => trans('validation.attributes.email'),
'password' => trans('validation.attributes.password'),
2023-12-18 10:30:19 +01:00
'password_confirmation' => trans('validation.attributes.password_confirmation'),
2024-01-26 16:24:14 +01:00
'ok_cgv' => trans('conditions d\'utilisation'),
'opt_in' => trans('newsletter'),
2023-12-15 15:53:55 +00:00
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
2024-01-26 16:24:14 +01:00
public function messages(): array
2023-12-15 15:53:55 +00:00
{
return [
2024-01-26 16:24:14 +01:00
'postal_code' => trans('validation.postal_code', [
2023-12-18 10:30:19 +01:00
'countries' => \implode(', ', Country::getValidCountriesLabel()), 'examples' => 72000
]),
2024-01-26 16:24:14 +01:00
'phone.phone' => trans('validation.phone', [
2023-12-18 10:30:19 +01:00
'countries' => \implode(', ', Country::getValidCountriesLabel())
2023-12-15 15:53:55 +00:00
]),
2024-01-26 16:24:14 +01:00
'email.unique' => trans('Un compte existe déjà avec cette adresse.'),
2023-12-15 15:53:55 +00:00
'ok_cgv.accepted' => trans('Vous devez accepter les conditions d\'utilisation du site pour continuer.')
];
}
}