ldc-tregalette/app/Http/Requests/Fo/Customers/CreateAccountRequest.php
2023-12-15 15:53:55 +00:00

109 lines
3.5 KiB
PHP

<?php
namespace App\Http\Requests\Fo\Customers;
use App\Rules\IsAdult;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rules\Password;
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([
'phone' => $this->phone,
'opt_in' => $this->opt_in ? true : false
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'phone' => 'nullable|max:255|phone:FR',
'address' => 'required|string|min:1|max:255',
'postcode' => 'required|string|min:1|postal_code:FR',
'city' => 'required|string|min:1|max:255',
'birth_date' => ['required', new IsAdult(trans('Vous devez être majeur pour vous inscrire.'))],
'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' => [
'required', 'confirmed', Password::min(8)->mixedCase()->numbers()->symbols()
],
'ok_cgv' => 'accepted',
'opt_in' => 'required|boolean',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'lastname' => trans('nom'),
'firstname' => trans('prénom'),
'phone' => trans('téléphone'),
'address' => trans('adresse'),
'postcode' => trans('code postal'),
'city' => trans('ville'),
'birth_date' => trans('date de naissance'),
'email' => trans('email'),
'password' => trans('mot de passe'),
'ok_cgv' => trans('conditions d\'utilisation'),
'opt_in' => trans('newsletter'),
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [
'postal_code' => trans('validation.custom.postal_code', [
'countries' => Country::france()->label, 'examples' => 72000
]),
'phone.phone' => trans('validation.custom.phone'),
'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.')
];
}
}