fix: 🐛 improve contact form to display input file when added

This commit is contained in:
Nicolas Demangeat 2024-11-27 11:52:18 +01:00
parent 3352142d54
commit 546e2d102e
2 changed files with 29 additions and 11 deletions

View file

@ -21,7 +21,7 @@ class ListenFormRequest extends FormRequest
protected function prepareForValidation(): void
{
$this->merge([
'country' => $this->post('country') == null ? $this->post('country') : null,
'country' => $this->post('country') ?? null,
'accept_rules' => $this->boolean('accept_rules'),
'send_data' => $this->boolean('send_data'),
]);
@ -41,20 +41,22 @@ class ListenFormRequest extends FormRequest
'society' => 'present|nullable|string|between:3,255',
'email' => 'required|email:rfc,strict,dns,spoof,filter|max:255',
// @phpstan-ignore-next-line
'phone' => 'present|nullable|' . (
Country::make(request()->country)->value === Country::france->value
) ? 'phone:FR|max:25' : 'number|max:50',
'phone' => [
'present',
'nullable',
!is_null(request()->country) ? (Country::make(request()->country)->value === Country::france->value ? 'phone:FR|max:25' : 'number|max:50') : 'number|max:50'
],
// @phpstan-ignore-next-line
'postal_code' => 'present|nullable|' . (
Country::make(request()->country)->value === Country::france->value
) ? 'postal_code:FR|max:25' : 'number|max:50',
'postal_code' => [
'present',
'nullable',
!is_null(request()->country) ? (Country::make(request()->country)->value === Country::france->value ? 'postal_code:FR|max:25' : 'number|max:50') : 'number|max:50'
],
'city' => 'present|nullable|string|between:3,20',
'country' => ['present', 'nullable', new Enum(Country::class)],
'address' => 'present|nullable|string|between:3,255',
'object' => ['required', new Enum(ObjectEnum::class)],
'message' => 'required|min:20|max:35656',
'accept_rules' => 'required|boolean',
'send_data' => 'required|boolean',
];
if (
isset(request()->file) and

View file

@ -136,7 +136,7 @@
</fieldset>
<fieldset class="col-12 mb-3">
<select class="form-control bg-transparent px-2" id="contactCountry" name="country">
<option value="null" selected>
<option selected disabled>
{{ __('Sélectionner un :entity', ['entity' => __('validation.attributes.country')]) }}
</option>
@foreach (\App\Enums\Country::toArray() as $key => $country)
@ -192,7 +192,7 @@
<div class="col-12 mb-1">
<label class="btn btn-light w-100 test d-flex justify-content-between text-start shadow-none"
for="contact-file">
{{ Str::of(__('joindre un fichier'))->ucFirst()->value() }}*
<span id="file-name">{{ Str::of(__('joindre un fichier'))->ucFirst()->value() }}</span>
<span id="contact-file-icon"></span>
</label>
<input class="d-none" id="contact-file" name="file" type="file" value="{{ old('file') }}"
@ -206,6 +206,11 @@
{{ Str::of(__('formats acceptés: jpg,jpeg,png,pdf'))->ucFirst()->value() }}
</span>
</div>
<div class="col-12 pe-md-5 m-xl-0 mb-2">
<span class="text-body-secondary">
{{ old('file') }}
</span>
</div>
</div>
</fieldset>
{{-- Don't remove next empty div, use for placement --}}
@ -276,6 +281,17 @@
e.preventDefault();
window.WebCookieNotice.acceptCookie('recaptcha');
});
// This part manage the file display
const fileInput = document.getElementById('contact-file');
const fileNameSpan = document.getElementById('file-name');
const defaultText = "{{ Str::of(__('joindre un fichier'))->ucFirst()->value() }}";
fileInput.addEventListener('change', function(e) {
if (this.files && this.files.length > 0) {
fileNameSpan.textContent = this.files[0].name;
} else {
fileNameSpan.textContent = defaultText;
}
});
});
</script>
@endpush