fix: 🐛 update validation rules on register customer for all valid countries

This commit is contained in:
Alexis Gousseau 2024-09-12 10:58:52 +02:00
parent e5df645adc
commit eebffd6172
3 changed files with 27 additions and 7 deletions

View file

@ -11,6 +11,13 @@ use LDC\LDCCRM\Models\Customer as CrmCustomer;
class StoreCustomerRequest extends FormRequest
{
/**
* Current country.
*
* @var \LDC\LDCCRM\Enums\Country|null
*/
protected $currentCountry = null;
/**
* Determine if the user is authorized to make this request.
*
@ -28,6 +35,11 @@ class StoreCustomerRequest extends FormRequest
*/
public function prepareForValidation()
{
try {
$this->currentCountry = Country::make(\intval($this->country));
} catch (\TypeError | \BadMethodCallException $e) {
$this->currentCountry = null;
}
$this->merge([
'phone' => $this->input('phone'),
'opt_in' => $this->boolean('opt_in'),
@ -42,19 +54,25 @@ class StoreCustomerRequest extends FormRequest
*/
public function rules()
{
$countryList = implode(',', Country::getValidIsoAplha2Codes());
return [
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'username' => 'required|string|max:255',
'phone' => 'nullable|max:255|phone:FR',
'phone' => "nullable|max:255|between:1,100|phone:{$countryList}",
'address' => 'required|string|min:1|max:255',
'address2' => 'nullable|string|min:1|max:255',
'postal_code' => 'required|string|min:1|postal_code:FR',
'postal_code' => "required|string|min:1,100|postal_code:{$countryList}",
'city' => 'required|string|min:2|max:255',
'birth_date' => 'required|date',
'country' => ['required', Rule::enum(Country::class), Rule::in(Country::getValidCountries())],
'country' => ['required', Rule::enum(Country::class), sprintf(
'in:%s',
implode(',', Country::getValidCountriesValues())
)],
'email' => [
'required', 'email:rfc,dns,spoof', 'max:255',
'required',
'email:rfc,dns,spoof',
'max:255',
// * Does email already exists in CRM ?
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, $value, \Closure $fail) {
@ -65,7 +83,9 @@ class StoreCustomerRequest extends FormRequest
'unique:customers,email',
],
'password' => [
'required', 'confirmed', Password::min(8)->mixedCase()->numbers()->symbols()->uncompromised()
'required',
'confirmed',
Password::min(8)->mixedCase()->numbers()->symbols()->uncompromised()
],
'ok_cgu' => 'accepted',
'opt_in' => 'required|boolean',

View file

@ -14,7 +14,7 @@
"require": {
"php": "^8.3",
"ext-bcmath": "*",
"axlon/laravel-postal-code-validation": "^3.6",
"axlon/laravel-postal-code-validation": "^3.7",
"carlos-meneses/laravel-mpdf": "^2.1",
"diglactic/laravel-breadcrumbs": "^9.0",
"guzzlehttp/guzzle": "^7.2",

2
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "68cf851912d3efc0b7840129870bc909",
"content-hash": "db80cce9d97851f47e3720647bcf6138",
"packages": [
{
"name": "axlon/laravel-postal-code-validation",