fix: move crm subject management from front to controller in contact form

This commit is contained in:
Noah Lancien 2025-12-30 15:33:23 +01:00
parent 0ffea2d827
commit f478f1f262
5 changed files with 2 additions and 20 deletions

View file

@ -74,7 +74,7 @@ class ContactController extends Controller
$validatedData['email'],
$validatedData['message'],
config('crm.brand'),
$validatedData['crmsubject'],
ContactFormType::make($validatedData['type'])->crmId(),
$validatedData['address'],
$validatedData['town'],
$validatedData['postal'],

View file

@ -47,7 +47,6 @@ class ContactFormRequest extends FormRequest
'town' => 'required|min:3|max:90',
'phone' => 'required|phone:FR',
'message' => 'required|min:20|max:1900',
'crmsubject' => ['required', Rule::in(array_values(ContactFormType::crmIds()))],
];
if (ContactFormType::make($this->input('type'))->equals(ContactFormType::complain)) {
@ -102,7 +101,6 @@ class ContactFormRequest extends FormRequest
'consumptionDateMonth' => sprintf('%s - %s', trans('validation.custom.consumption_date'), trans('month')),
'consumptionDateYear' => sprintf('%s - %s', trans('validation.custom.consumption_date'), trans('year')),
'joinFile' => trans('validation.custom.attachment'),
'crmsubject' => trans('validation.custom.crmsubject'),
];
}
}

View file

@ -2,34 +2,21 @@
document.addEventListener("DOMContentLoaded", function () {
const checkedSubject = document.querySelector("input[name=\"type\"]:checked") as HTMLInputElement;
const typeRadioInputs = document.querySelectorAll("input[name=\"type\"]") as NodeListOf<HTMLInputElement>;
const crmSubjectInput = document.querySelector("input[name=\"crmsubject\"]") as HTMLInputElement;
if (!document.querySelector("#contact-form") || !checkedSubject || !typeRadioInputs) {
return;
}
checkRadioChecked(checkedSubject.value);
updateCrmSubjectValue(checkedSubject, crmSubjectInput);
for (const typeRadioInput of typeRadioInputs) {
typeRadioInput.addEventListener("change", (e: Event) => {
if (e.currentTarget instanceof HTMLInputElement) {
checkRadioChecked(e.currentTarget.value);
updateCrmSubjectValue(e.currentTarget, crmSubjectInput);
}
});
}
});
/**
* Update the crm subject value depending on checked radio input.
* @param input HTMLInputElement
* @param crmSubjectInput HTMLInputElement
* @return void
*/
function updateCrmSubjectValue(input: HTMLInputElement, crmSubjectInput: HTMLInputElement): void {
crmSubjectInput.value = String(input.getAttribute("data-crm-subject"));
}
function checkRadioChecked(value: string) {
// Display complain form if the current checked radio is equal to ContactFormType::complain.
displayComplainForm(value === "3");

View file

@ -76,8 +76,6 @@
</div>
@endif
</div>
{{-- CRM SUBJECT --}}
<input class="d-none" name="crmsubject" value="{{ \App\Enums\ContactFormType::complain->crmId() }}">
{{-- SUBMIT BUTTON --}}
<div class="d-flex justify-content-end">
<button class="g-recaptcha btn btn-secondary ms-auto"

View file

@ -7,8 +7,7 @@
<div class="col-12 col-md-6 col-xl-12 mb-3 h-auto">
<div class="radio-subject position-relative h-100">
<input id="{{ $contactType->name }}" name="type" type="radio" value="{{ $contactType->value }}"
@checked($loop->first || ($contactType->value === intval(old('type')) && !is_null(old('type'))))
data-crm-subject="{{ $contactType->crmId() }}" />
@checked($loop->first || ($contactType->value === intval(old('type')) && !is_null(old('type')))) />
<label class="d-flex align-items-center" for="{{ $contactType->name }}">
<img class="me-3" src="{{ Vite::asset($contactType->picto()['src']) }}" alt="{{ $contactType->picto()['alt'] }}">
<p class="mb-0 h-fit">