2023-11-29 10:57:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Fo\Customers;
|
|
|
|
|
|
|
|
|
|
use App\Rules\IsAdult;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-12-08 18:55:31 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2023-11-29 10:57:48 +01:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2023-12-08 18:55:31 +01:00
|
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
2023-11-29 10:57:48 +01: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
|
|
|
|
|
*/
|
|
|
|
|
public function authorize()
|
|
|
|
|
{
|
|
|
|
|
return Auth::guard('frontend')->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function prepareForValidation()
|
|
|
|
|
{
|
|
|
|
|
$this->merge([
|
2023-12-11 11:21:06 +01:00
|
|
|
'phone' => $this->input('phone'),
|
|
|
|
|
'opt_in' => $this->boolean('opt_in'),
|
|
|
|
|
'ok_cgv' => $this->boolean('ok_cgv')
|
2023-11-29 10:57:48 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules()
|
|
|
|
|
{
|
2024-06-25 16:50:56 +02:00
|
|
|
$countryEnum = Country::make($this->integer('country'));
|
|
|
|
|
|
2023-11-29 10:57:48 +01:00
|
|
|
return [
|
|
|
|
|
'lastname' => 'required|string|max:255',
|
|
|
|
|
'firstname' => 'required|string|max:255',
|
|
|
|
|
'phone' => 'nullable|max:255|phone:FR',
|
|
|
|
|
'address' => 'required|string|min:1|max:255',
|
2023-12-13 10:01:52 +01:00
|
|
|
'address2' => 'nullable|string|min:1|max:255',
|
2024-06-25 16:50:56 +02:00
|
|
|
'postal_code' => [
|
|
|
|
|
'required',
|
|
|
|
|
'string',
|
|
|
|
|
'min:1',
|
|
|
|
|
$countryEnum->equals(Country::belgium()) ?
|
|
|
|
|
'postal_code:BE' :
|
|
|
|
|
'',
|
|
|
|
|
$countryEnum->equals(Country::luxembourg()) ?
|
|
|
|
|
'postal_code:LU' :
|
|
|
|
|
'',
|
|
|
|
|
(!$countryEnum->equals(Country::belgium()) && !$countryEnum->equals(Country::luxembourg())) ?
|
|
|
|
|
'postal_code:FR' :
|
|
|
|
|
'',
|
|
|
|
|
],
|
2023-12-11 11:21:06 +01:00
|
|
|
'city' => 'required|string|min:2|max:255',
|
2023-11-29 10:57:48 +01:00
|
|
|
'birth_date' => ['required', new IsAdult(trans('Vous devez être majeur pour vous inscrire.'))],
|
2023-12-08 18:55:31 +01:00
|
|
|
'country' => [
|
|
|
|
|
'required', new EnumIsValidRule(Country::class),
|
|
|
|
|
Rule::in(Country::getValidCountries())
|
|
|
|
|
],
|
2023-11-29 10:57:48 +01:00
|
|
|
'email' => [
|
|
|
|
|
'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',
|
|
|
|
|
],
|
|
|
|
|
'password' => [
|
2023-12-05 12:33:25 +01:00
|
|
|
'required', 'confirmed', Password::min(8)->mixedCase()
|
|
|
|
|
->numbers()->symbols()->uncompromised()
|
2023-11-29 10:57:48 +01:00
|
|
|
],
|
|
|
|
|
'ok_cgv' => 'accepted',
|
|
|
|
|
'opt_in' => 'required|boolean',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get custom attributes for validator errors.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function attributes()
|
|
|
|
|
{
|
|
|
|
|
return [
|
2023-12-08 18:55:31 +01:00
|
|
|
'lastname' => trans('validation.attributes.first_name'),
|
|
|
|
|
'firstname' => trans('validation.attributes.first_name'),
|
|
|
|
|
'phone' => trans('validation.attributes.phone'),
|
2023-12-13 10:01:52 +01:00
|
|
|
'address' => trans('validation.attributes.line_address_1'),
|
|
|
|
|
'address2' => trans('validation.attributes.line_address_2'),
|
2023-12-08 18:55:31 +01:00
|
|
|
'country' => trans('validation.attributes.country'),
|
2023-12-13 10:01:52 +01:00
|
|
|
'postal_code' => trans('validation.attributes.postal_code'),
|
2023-12-08 18:55:31 +01:00
|
|
|
'city' => trans('validation.attributes.city'),
|
|
|
|
|
'birth_date' => trans('validation.attributes.date_of_birth'),
|
|
|
|
|
'email' => trans('validation.attributes.email'),
|
|
|
|
|
'password' => trans('validation.attributes.password'),
|
|
|
|
|
'password_confirmation' => trans('validation.attributes.password_confirmation'),
|
2023-11-29 10:57:48 +01:00
|
|
|
'ok_cgv' => trans('conditions d\'utilisation'),
|
|
|
|
|
'opt_in' => trans('newsletter'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get custom messages for validator errors.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function messages()
|
|
|
|
|
{
|
2024-06-25 16:50:56 +02:00
|
|
|
$messages = [
|
2023-12-13 10:01:52 +01:00
|
|
|
'postal_code' => trans('validation.postal_code', [
|
|
|
|
|
'countries' => \implode(', ', Country::getValidCountriesLabel()), 'examples' => 72000
|
|
|
|
|
]),
|
|
|
|
|
'phone.phone' => trans('validation.phone', [
|
|
|
|
|
'countries' => \implode(', ', Country::getValidCountriesLabel())
|
2023-11-29 10:57:48 +01:00
|
|
|
]),
|
|
|
|
|
'email.unique' => trans('Un compte existe déjà avec cette adresse.'),
|
|
|
|
|
'ok_cgv.accepted' => trans('Vous devez accepter les conditions d\'utilisation du site pour continuer.')
|
|
|
|
|
];
|
2024-06-25 16:50:56 +02:00
|
|
|
|
|
|
|
|
$countryEnum = Country::make($this->integer('country'));
|
|
|
|
|
if ($countryEnum->equals(Country::belgium())) {
|
|
|
|
|
$messages = array_merge(
|
|
|
|
|
$messages,
|
|
|
|
|
[
|
|
|
|
|
'postal_code' => trans('validation.postal_code', [
|
|
|
|
|
'countries' => Country::belgium()->label, 'examples' => 1000
|
|
|
|
|
]),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} elseif ($countryEnum->equals(Country::luxembourg())) {
|
|
|
|
|
$messages = array_merge(
|
|
|
|
|
$messages,
|
|
|
|
|
[
|
|
|
|
|
'postal_code' => trans('validation.postal_code', [
|
|
|
|
|
'countries' => Country::luxembourg()->label, 'examples' => 1111
|
|
|
|
|
]),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$messages = array_merge(
|
|
|
|
|
$messages,
|
|
|
|
|
[
|
|
|
|
|
'postal_code' => trans('validation.postal_code', [
|
|
|
|
|
'countries' => \implode(', ', Country::getValidCountriesLabel()), 'examples' => 72000
|
|
|
|
|
]),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} //end if
|
|
|
|
|
|
|
|
|
|
return $messages;
|
2023-11-29 10:57:48 +01:00
|
|
|
}
|
|
|
|
|
}
|