diff --git a/app/components/editable_champ/siret_component/siret_component.html.haml b/app/components/editable_champ/siret_component/siret_component.html.haml index 9e0745c79..456afd069 100644 --- a/app/components/editable_champ/siret_component/siret_component.html.haml +++ b/app/components/editable_champ/siret_component/siret_component.html.haml @@ -1,4 +1,4 @@ -= @form.text_field(:value, input_opts(id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'turbo-input', turbo_input_load_on_connect_value: @champ.prefilled? && @champ.value.present? && @champ.etablissement.blank?, turbo_input_url_value: update_path }, required: @champ.required?, pattern: "[0-9]{14}", class: "width-33-desktop", maxlength: 14)) += @form.text_field(:value, input_opts(id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'turbo-input format', format: 'siret', turbo_input_load_on_connect_value: @champ.prefilled? && @champ.value.present? && @champ.etablissement.blank?, turbo_input_url_value: update_path }, required: @champ.required?, class: "width-33-desktop")) .siret-info{ id: dom_id(@champ, :siret_info) } - if @champ.etablissement.present? = render EditableChamp::EtablissementTitreComponent.new(etablissement: @champ.etablissement) diff --git a/app/javascript/controllers/format_controller.ts b/app/javascript/controllers/format_controller.ts index d5712d46e..9eacd690e 100644 --- a/app/javascript/controllers/format_controller.ts +++ b/app/javascript/controllers/format_controller.ts @@ -11,6 +11,13 @@ export class FormatController extends ApplicationController { replaceValue(target, value); }); break; + case 'siret': + this.on('input', (event) => { + const target = event.target as HTMLInputElement; + const value = this.formatSIRET(target.value); + replaceValue(target, value); + }); + break; case 'iban': this.on('input', (event) => { const target = event.target as HTMLInputElement; @@ -38,7 +45,12 @@ export class FormatController extends ApplicationController { private formatList(value: string) { return value.replace(/;/g, ','); } - + private formatSIRET(value: string) { + return value + .replace(/[^\d]/gi, '') + .replace(/^\s*(\d{3})\s*(\d{3})\s*(\d{3})\s*(\d{5})\s*$/gi, '$1 $2 $3 $4') + .trim(); + } private formatIBAN(value: string) { return value .replace(/[^\dA-Z]/gi, '') diff --git a/config/locales/models/champs/siret_champ/en.yml b/config/locales/models/champs/siret_champ/en.yml index 4fa59a07e..a4104f3be 100644 --- a/config/locales/models/champs/siret_champ/en.yml +++ b/config/locales/models/champs/siret_champ/en.yml @@ -3,4 +3,4 @@ en: attributes: champs/siret_champ: hints: - value: "Enter 14 digits, no space. Example : 50000123456789" + value: "Enter 14 digits. Example : 500 0012 345 6789" diff --git a/config/locales/models/champs/siret_champ/fr.yml b/config/locales/models/champs/siret_champ/fr.yml index 2c8f73bdc..d38d7b591 100644 --- a/config/locales/models/champs/siret_champ/fr.yml +++ b/config/locales/models/champs/siret_champ/fr.yml @@ -3,4 +3,4 @@ fr: attributes: champs/siret_champ: hints: - value: "Saisissez 14 chiffres sans espaces. Exemple : 50000123456789" + value: "Saisissez 14 chiffres. Exemple : 500 001 234 56789"