ldc-tregalette/app/Http/Requests/Fo/ContactFormRequest.php

124 lines
4.1 KiB
PHP
Raw Normal View History

2023-12-15 15:53:55 +00:00
<?php
namespace App\Http\Requests\Fo;
use App\Enums\Contact\ObjectEnum;
use App\Enums\Country;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;
class ContactFormRequest extends FormRequest
{
/**
* Prepare the data for validation.
*
* @return void
*/
public function prepareForValidation(): void
{
$this->merge([
// * present fields
'first_name' => Str::of($this->post('first_name'))->value(),
'phone' => Str::of($this->post('phone'))->value(),
'address' => Str::of($this->post('address'))->value(),
'city' => Str::of($this->post('city'))->value(),
'postal_code' => Str::of($this->post('postal_code'))->value(),
'message' => Str::of($this->post('message'))->value(),
// * boolean
'terms' => $this->post('terms') ? true : false,
// * integer
'batch_number' => $this->post('batch_number') ? intval($this->post('batch_number')) : null,
]);
}
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
ToolboxHelper::validateRecaptcha($this, \config('services.google.recaptcha.version'));
return [
// * Has to be present fields
'type' => ['required', new Enum(ObjectEnum::class)],
'first_name' => 'present|max:150',
// * obligatoire
'last_name' => 'required|min:2|max:150',
'phone' => 'present|phone:FR|max:25',
// * obligatoire
'email' => 'required|email:rfc,dns,spoof|max:255',
'address' => 'present|max:255',
'city' => 'present|max:255',
'postal_code' => 'present|min:0|postal_code:FR|max:25',
'message' => 'present|min:20|max:35656',
'file' => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:2048',
// * obligatoire
'terms' => 'required|boolean|accepted',
// * Conditional fields
// * obligatoire si type == product
'product' => [
Rule::requiredIf($this->post('type') == ObjectEnum::reclaim->value()),
'nullable',
'exists:products,id'
],
'expiration_date' => [
'nullable',
Rule::prohibitedIf($this->post('type') != ObjectEnum::reclaim->value()),
'date_format:Y-m-d'
],
'batch_number' => [
'nullable',
Rule::prohibitedIf($this->post('type') != ObjectEnum::reclaim->value()),
'digits_between:3,4'
]
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'subject' => trans('validation.attributes.subject'),
'file' => trans('validation.custom.attachment'),
'terms' => trans('validation.attributes.terms'),
'expiration_date' => trans('validation.custom.expiration_date'),
'batch_number' => trans('validation.custom.batch_number'),
'job_title' => trans('validation.custom.job_title'),
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages(): array
{
return [
'postal_code' => trans('validation.custom.postal_code', [
'countries' => Country::france->label(), 'examples' => 72000
]),
'phone.phone' => trans('validation.custom.phone'),
];
}
}