ldc-legaulois/app/Http/Requests/CustomerRegisterRequest.php

100 lines
3.5 KiB
PHP
Raw Normal View History

2021-03-05 08:17:35 +01:00
<?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;
2021-03-05 08:17:35 +01:00
class CustomerRegisterRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
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
]);
2021-03-05 08:17:35 +01:00
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',
2021-03-05 08:17:35 +01:00
'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),
2021-03-21 22:51:19 +01:00
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
2021-03-05 08:17:35 +01:00
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',
2021-03-05 08:17:35 +01:00
'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',
2021-05-26 17:18:30 +02:00
'agree' => 'required'
2021-03-05 08:17:35 +01:00
];
}
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.')
];
}
2021-03-05 08:17:35 +01:00
}