99 lines
3.5 KiB
PHP
99 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
|
use LDC\LDCCRM\Enums\Country;
|
|
|
|
class CustomerRegisterRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
$this->merge([
|
|
'birth_date' => sprintf(
|
|
'%d-%s-%s',
|
|
$this->year,
|
|
str_pad($this->month, 2, '0', \STR_PAD_LEFT),
|
|
str_pad($this->day, 2, '0', \STR_PAD_LEFT),
|
|
),
|
|
'opt_in' => $this->opt_in ? true : false
|
|
]);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'email' => 'required|email:rfc,dns,spoof,filter|max:255|unique:customers,email',
|
|
'username' => 'required|string|min:1|max:255|unique:customers,username',
|
|
'firstname' => 'required|string|min:1|max:255',
|
|
'lastname' => 'required|string|max:255',
|
|
'day' => 'required|alpha_num|gt:0|lt:32',
|
|
'month' => 'required|alpha_num|gt:0|lt:13',
|
|
'year' => [
|
|
'required', 'alpha_num', 'gt:' . (date('Y') - 101), 'lt:' . (date('Y') + 1),
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
$birthDate = Carbon::createFromDate($this->year, $this->month, $this->day);
|
|
if (
|
|
$birthDate->format('dmY') !== $this->day . $this->month . $this->year ||
|
|
$birthDate > Carbon::now()
|
|
) {
|
|
$fail(__('Veuillez entrer une date de naissance valide'));
|
|
}
|
|
}
|
|
],
|
|
'birth_date' => 'required|date_format:Y-m-d',
|
|
'address' => 'required|min:2|max:255',
|
|
'phone' => 'sometimes|max:255',
|
|
'city' => 'required|string|min:2|max:255',
|
|
'country' => [
|
|
'required', new EnumIsValidRule(Country::class)
|
|
],
|
|
'postcode' => 'required|numeric|digits_between:4,5',
|
|
'password' => [
|
|
'required', 'confirmed', 'min:8', 'max:255',
|
|
Password::min(8)
|
|
->mixedCase()
|
|
->letters()
|
|
->numbers()
|
|
->symbols()
|
|
->uncompromised()
|
|
],
|
|
'opt_in' => 'required|boolean',
|
|
'agree' => 'required'
|
|
];
|
|
}
|
|
|
|
public function attributes()
|
|
{
|
|
return [
|
|
'postcode' => 'Code postal'
|
|
];
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
return [
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'agree.required' => trans('Vous devez accepter les conditions d\'utilisations pour pouvoir vous créer un compte.'),
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'postcode.postal_code' => trans('Le champ :attribute accepte les codes postaux des pays suivants :countries comme par exemple :examples.'),
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'phone.phone' => trans('Le champ :attribute est invalide, il accepte les numéros des pays suivants France, Belgique, Luxembourg.')
|
|
];
|
|
}
|
|
}
|