refactor(champ): add required?

used for the `required` html attribute. It check visibility to avoid hidden required input which prevent the form from being sent.
This commit is contained in:
simon lehericey 2022-10-17 10:05:59 +02:00 committed by LeSim
parent c94b1ce3d9
commit 528b7ec3f8
26 changed files with 35 additions and 28 deletions

View file

@ -1,6 +1,6 @@
= @form.hidden_field :value = @form.hidden_field :value
= @form.hidden_field :external_id = @form.hidden_field :external_id
= react_component("ComboAdresseSearch", = react_component("ComboAdresseSearch",
required: @champ.mandatory?, required: @champ.required?,
id: @champ.input_id, id: @champ.input_id,
describedby: @champ.describedby_id) describedby: @champ.describedby_id)

View file

@ -1,6 +1,6 @@
= @form.hidden_field :value = @form.hidden_field :value
= @form.hidden_field :external_id = @form.hidden_field :external_id
= react_component("ComboAnnuaireEducationSearch", = react_component("ComboAnnuaireEducationSearch",
required: @champ.mandatory?, required: @champ.required?,
id: @champ.input_id, id: @champ.input_id,
describedby: @champ.describedby_id) describedby: @champ.describedby_id)

View file

@ -1,4 +1,4 @@
= @form.check_box :value, = @form.check_box :value,
{ required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }, { required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } },
'on', 'on',
'off' 'off'

View file

@ -3,7 +3,7 @@
= @form.label :numero_allocataire, t('.numero_allocataire_label') = @form.label :numero_allocataire, t('.numero_allocataire_label')
%p.notice= t('.numero_allocataire_notice') %p.notice= t('.numero_allocataire_notice')
= @form.text_field :numero_allocataire, = @form.text_field :numero_allocataire,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
class: "width-33-desktop" class: "width-33-desktop"
@ -11,6 +11,6 @@
= @form.label :code_postal, t('.code_postal_label') = @form.label :code_postal, t('.code_postal_label')
%p.notice= t('.code_postal_notice') %p.notice= t('.code_postal_notice')
= @form.text_field :code_postal, = @form.text_field :code_postal,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
class: "width-33-desktop" class: "width-33-desktop"

View file

@ -3,7 +3,7 @@
= @form.hidden_field :departement = @form.hidden_field :departement
= @form.hidden_field :code_departement = @form.hidden_field :code_departement
= react_component("ComboCommunesSearch", = react_component("ComboCommunesSearch",
required: @champ.mandatory?, required: @champ.required?,
id: @champ.input_id, id: @champ.input_id,
classNameDepartement: "width-33-desktop width-100-mobile", classNameDepartement: "width-33-desktop width-100-mobile",
className: "width-66-desktop width-100-mobile", className: "width-66-desktop width-100-mobile",

View file

@ -2,6 +2,6 @@
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
value: @champ.value, value: @champ.value,
required: @champ.mandatory?, required: @champ.required?,
placeholder: 'aaaa-mm-jj', placeholder: 'aaaa-mm-jj',
class: "width-33-desktop" class: "width-33-desktop"

View file

@ -3,4 +3,4 @@
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
step: :any, step: :any,
placeholder: "3.14", placeholder: "3.14",
required: @champ.mandatory? required: @champ.required?

View file

@ -1,7 +1,7 @@
= @form.hidden_field :value = @form.hidden_field :value
= @form.hidden_field :external_id = @form.hidden_field :external_id
= react_component("ComboDepartementsSearch", = react_component("ComboDepartementsSearch",
required: @champ.mandatory?, required: @champ.required?,
id: @champ.input_id, id: @champ.input_id,
className: "width-33-desktop width-100-mobile", className: "width-33-desktop width-100-mobile",
describedby: @champ.describedby_id) describedby: @champ.describedby_id)

View file

@ -3,7 +3,7 @@
= @form.label :numero_fiscal, t('.numero_fiscal_label') = @form.label :numero_fiscal, t('.numero_fiscal_label')
%p.notice= t('.numero_fiscal_notice') %p.notice= t('.numero_fiscal_notice')
= @form.text_field :numero_fiscal, = @form.text_field :numero_fiscal,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
class: "width-33-desktop" class: "width-33-desktop"
@ -11,6 +11,6 @@
= @form.label :reference_avis, t('.reference_avis_label') = @form.label :reference_avis, t('.reference_avis_label')
%p.notice= t('.reference_avis_notice') %p.notice= t('.reference_avis_notice')
= @form.text_field :reference_avis, = @form.text_field :reference_avis,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
class: "width-33-desktop" class: "width-33-desktop"

View file

@ -4,7 +4,7 @@
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: "Numéro de dossier", placeholder: "Numéro de dossier",
autocomplete: 'off', autocomplete: 'off',
required: @champ.mandatory?, required: @champ.required?,
data: { controller: 'turbo-input', turbo_input_url_value: champs_dossier_link_path(@champ.id) }, data: { controller: 'turbo-input', turbo_input_url_value: champs_dossier_link_path(@champ.id) },
class: "width-33-desktop" class: "width-33-desktop"

View file

@ -2,4 +2,4 @@
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: t(".placeholder"), placeholder: t(".placeholder"),
required: @champ.mandatory? required: @champ.required?

View file

@ -1,4 +1,4 @@
= @form.check_box :value, = @form.check_box :value,
{ required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }, { required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } },
'on', 'on',
'off' 'off'

View file

@ -1,7 +1,7 @@
= @form.text_field :value, = @form.text_field :value,
id: @champ.input_id, id: @champ.input_id,
placeholder: t(".placeholder"), placeholder: t(".placeholder"),
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
data: { controller: 'iban-input'}, data: { controller: 'iban-input'},
class: "width-66-desktop", class: "width-66-desktop",

View file

@ -2,4 +2,4 @@
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: 5, placeholder: 5,
required: @champ.mandatory? required: @champ.required?

View file

@ -2,7 +2,7 @@
= @form.select :primary_value, = @form.select :primary_value,
@champ.primary_options, @champ.primary_options,
{}, {},
{ data: { secondary_options: @champ.secondary_options }, required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } } { data: { secondary_options: @champ.secondary_options }, required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }
.secondary{ class: @champ.has_secondary_options_for_primary? ? '' : 'hidden' } .secondary{ class: @champ.has_secondary_options_for_primary? ? '' : 'hidden' }
= @form.label :secondary_value, for: "#{@champ.input_id}-secondary" do = @form.label :secondary_value, for: "#{@champ.input_id}-secondary" do
@ -14,5 +14,5 @@
= @form.select :secondary_value, = @form.select :secondary_value,
@champ.secondary_options[@champ.primary_value], @champ.secondary_options[@champ.primary_value],
{}, {},
{ data: { secondary: true }, disabled: !@champ.has_secondary_options_for_primary?, required: @champ.mandatory?, id: "#{@champ.input_id}-secondary", aria: { describedby: "#{@champ.describedby_id}-secondary" } } { data: { secondary: true }, disabled: !@champ.has_secondary_options_for_primary?, required: @champ.required?, id: "#{@champ.input_id}-secondary", aria: { describedby: "#{@champ.describedby_id}-secondary" } }
= @form.hidden_field :secondary_value, value: '', disabled: @champ.has_secondary_options_for_primary? = @form.hidden_field :secondary_value, value: '', disabled: @champ.has_secondary_options_for_primary?

View file

@ -3,7 +3,7 @@
= @form.label :ine, t('.ine_label') = @form.label :ine, t('.ine_label')
%p.notice= t('.ine_notice') %p.notice= t('.ine_notice')
= @form.text_field :ine, = @form.text_field :ine,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
class: "width-33-desktop" class: "width-33-desktop"

View file

@ -2,4 +2,4 @@
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: @champ.libelle, placeholder: @champ.libelle,
required: @champ.mandatory? required: @champ.required?

View file

@ -1,7 +1,7 @@
= @form.hidden_field :value = @form.hidden_field :value
= @form.hidden_field :external_id = @form.hidden_field :external_id
= react_component("ComboPaysSearch", = react_component("ComboPaysSearch",
required: @champ.mandatory?, required: @champ.required?,
id: @champ.input_id, id: @champ.input_id,
className: "width-33-desktop width-100-mobile", className: "width-33-desktop width-100-mobile",
describedby: @champ.describedby_id) describedby: @champ.describedby_id)

View file

@ -5,5 +5,5 @@
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: t(".placeholder"), placeholder: t(".placeholder"),
required: @champ.mandatory?, required: @champ.required?,
pattern: "[^a-z^A-Z]+" pattern: "[^a-z^A-Z]+"

View file

@ -3,6 +3,6 @@
= @form.label :identifiant, t('.identifiant_label') = @form.label :identifiant, t('.identifiant_label')
%p.notice= t('.identifiant_notice') %p.notice= t('.identifiant_notice')
= @form.text_field :identifiant, = @form.text_field :identifiant,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
class: "width-33-desktop" class: "width-33-desktop"

View file

@ -1,7 +1,7 @@
= @form.hidden_field :value = @form.hidden_field :value
= @form.hidden_field :external_id = @form.hidden_field :external_id
= react_component("ComboRegionsSearch", = react_component("ComboRegionsSearch",
required: @champ.mandatory?, required: @champ.required?,
id: @champ.input_id, id: @champ.input_id,
className: "width-33-desktop width-100-mobile", className: "width-33-desktop width-100-mobile",
describedby: @champ.describedby_id) describedby: @champ.describedby_id)

View file

@ -3,7 +3,7 @@
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: t(".placeholder"), placeholder: t(".placeholder"),
data: { controller: 'turbo-input', turbo_input_url_value: champs_rna_path(@champ.id) }, data: { controller: 'turbo-input', turbo_input_url_value: champs_rna_path(@champ.id) },
required: @champ.mandatory?, required: @champ.required?,
pattern: "W[0-9]{9}", pattern: "W[0-9]{9}",
title: t(".title"), title: t(".title"),
class: "width-33-desktop", class: "width-33-desktop",

View file

@ -3,7 +3,7 @@
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
placeholder: t(".placeholder"), placeholder: t(".placeholder"),
data: { controller: 'turbo-input', turbo_input_url_value: champs_siret_path(@champ.id) }, data: { controller: 'turbo-input', turbo_input_url_value: champs_siret_path(@champ.id) },
required: @champ.mandatory?, required: @champ.required?,
pattern: "[0-9]{14}", pattern: "[0-9]{14}",
title: t(".title"), title: t(".title"),
class: "width-33-desktop", class: "width-33-desktop",

View file

@ -1,4 +1,4 @@
= @form.text_field :value, = @form.text_field :value,
id: @champ.input_id, id: @champ.input_id,
required: @champ.mandatory?, required: @champ.required?,
aria: { describedby: @champ.describedby_id } aria: { describedby: @champ.describedby_id }

View file

@ -2,5 +2,5 @@
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
rows: 6, rows: 6,
required: @champ.mandatory?, required: @champ.required?,
value: html_to_string(@champ.value) value: html_to_string(@champ.value)

View file

@ -97,6 +97,13 @@ class Champ < ApplicationRecord
type_de_champ.mandatory? && visible? type_de_champ.mandatory? && visible?
end end
# used for the `required` html attribute
# check visibility to avoid hidden required input
# which prevent the form from being sent.
def required?
type_de_champ.mandatory? && visible?
end
def mandatory_blank_and_visible? def mandatory_blank_and_visible?
mandatory? && blank? mandatory? && blank?
end end