Merge pull request #7922 from betagouv/fix_mandatory_options

fix(drop_down_list): corrige l'apparition de l'option 'non renseigné' dans une liste de choix obligatoire
This commit is contained in:
LeSim 2022-10-18 11:20:22 +02:00 committed by GitHub
commit 1d82d5f7bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 57 additions and 54 deletions

View file

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

View file

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

View file

@ -1,4 +1,4 @@
= @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',
'off'

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,4 +1,4 @@
= @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',
'off'

View file

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

View file

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

View file

@ -2,17 +2,15 @@
= @form.select :primary_value,
@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' }
= @form.label :secondary_value, for: "#{@champ.input_id}-secondary" do
- sanitize((@champ.drop_down_secondary_libelle.presence || "Valeur secondaire dépendant de la première") + (@champ.type_de_champ.mandatory? ? tag.span(' *', class: 'mandatory') : ''))
-# - if @champ.type_de_champ.mandatory?
-# %span.mandatory *
- if @champ.drop_down_secondary_description.present?
.notice{ id: "#{@champ.describedby_id}-secondary" }= string_to_html(@champ.drop_down_secondary_description)
= @form.select :secondary_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?

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -462,7 +462,7 @@ module Users
end
if dossier.en_construction?
errors += @dossier.check_mandatory_champs
errors += @dossier.check_mandatory_and_visible_champs
end
errors
@ -473,7 +473,7 @@ module Users
@dossier.valid?(**submit_validation_options)
errors += @dossier.errors.full_messages
errors += @dossier.check_mandatory_champs
errors += @dossier.check_mandatory_and_visible_champs
if should_fill_groupe_instructeur?
@dossier.assign_to_groupe_instructeur(defaut_groupe_instructeur)

View file

@ -60,6 +60,7 @@ class Champ < ApplicationRecord
:rna?,
:siret?,
:stable_id,
:mandatory?,
to: :type_de_champ
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
@ -93,11 +94,14 @@ class Champ < ApplicationRecord
@sections ||= dossier.sections_for(self)
end
def mandatory?
# 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?
mandatory? && blank?
end

View file

@ -28,7 +28,7 @@ class Champs::CheckboxChamp < Champs::YesNoChamp
true? ? 'on' : 'off'
end
def mandatory_blank_and_visible?
def mandatory_blank?
mandatory? && (blank? || !true?)
end
end

View file

@ -38,7 +38,7 @@ class Champs::PieceJustificativeChamp < Champ
# We dont know how to search inside documents yet
end
def mandatory_blank_and_visible?
def mandatory_blank?
mandatory? && !piece_justificative_file.attached?
end

View file

@ -24,7 +24,7 @@ class Champs::SiretChamp < Champ
etablissement.present? ? etablissement.search_terms : [value]
end
def mandatory_blank_and_visible?
def mandatory_blank?
mandatory? && Siret.new(siret: value).invalid?
end
end

View file

@ -32,7 +32,7 @@ class Champs::TitreIdentiteChamp < Champ
# We dont know how to search inside documents yet
end
def mandatory_blank_and_visible?
def mandatory_blank?
mandatory? && !piece_justificative_file.attached?
end

View file

@ -981,9 +981,10 @@ class Dossier < ApplicationRecord
champs.filter(&:titre_identite?).map(&:piece_justificative_file).each(&:purge_later)
end
def check_mandatory_champs
def check_mandatory_and_visible_champs
(champs + champs.filter(&:block?).filter(&:visible?).flat_map(&:champs))
.filter(&:mandatory_blank_and_visible?)
.filter(&:visible?)
.filter(&:mandatory_blank?)
.map do |champ|
"Le champ #{champ.libelle.truncate(200)} doit être rempli."
end

View file

@ -1,26 +1,26 @@
shared_examples 'champ_spec' do
describe 'mandatory_blank_and_visible?' do
describe 'mandatory_blank?' do
let(:type_de_champ) { build(:type_de_champ, mandatory: mandatory) }
let(:champ) { build(:champ, type_de_champ: type_de_champ, value: value) }
let(:value) { '' }
let(:mandatory) { true }
context 'when mandatory and blank' do
it { expect(champ.mandatory_blank_and_visible?).to be(true) }
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when carte mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) }
let(:champ) { build(:champ_carte, type_de_champ: type_de_champ, value: value) }
let(:value) { nil }
it { expect(champ.mandatory_blank_and_visible?).to be(true) }
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when multiple_drop_down_list mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, mandatory: mandatory) }
let(:champ) { build(:champ_multiple_drop_down_list, type_de_champ: type_de_champ, value: value) }
let(:value) { '[]' }
it { expect(champ.mandatory_blank_and_visible?).to be(true) }
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when repetition blank' do
@ -39,18 +39,18 @@ shared_examples 'champ_spec' do
context 'when not blank' do
let(:value) { 'yop' }
it { expect(champ.mandatory_blank_and_visible?).to be(false) }
it { expect(champ.mandatory_blank?).to be(false) }
end
context 'when not mandatory' do
let(:mandatory) { false }
it { expect(champ.mandatory_blank_and_visible?).to be(false) }
it { expect(champ.mandatory_blank?).to be(false) }
end
context 'when not mandatory or blank' do
let(:value) { 'u' }
let(:mandatory) { false }
it { expect(champ.mandatory_blank_and_visible?).to be(false) }
it { expect(champ.mandatory_blank?).to be(false) }
end
end

View file

@ -78,7 +78,7 @@ describe Champs::LinkedDropDownListChamp do
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_list_value: value) }
it 'blank is fine' do
is_expected.not_to be_mandatory_blank_and_visible
is_expected.not_to be_mandatory_blank
end
end
@ -86,27 +86,27 @@ describe Champs::LinkedDropDownListChamp do
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: true, drop_down_list_value: value) }
context 'when there is no value' do
it { is_expected.to be_mandatory_blank_and_visible }
it { is_expected.to be_mandatory_blank }
end
context 'when there is a primary value' do
before { subject.primary_value = 'Primary' }
context 'when there is no secondary value' do
it { is_expected.to be_mandatory_blank_and_visible }
it { is_expected.to be_mandatory_blank }
end
context 'when there is a secondary value' do
before { subject.secondary_value = 'Secondary' }
it { is_expected.not_to be_mandatory_blank_and_visible }
it { is_expected.not_to be_mandatory_blank }
end
context 'when there is nothing to select for the secondary value' do
let(:value) { "--A--\nAbbott\nAbelard\n--B--\n--C--\nCynthia" }
before { subject.primary_value = 'B' }
it { is_expected.not_to be_mandatory_blank_and_visible }
it { is_expected.not_to be_mandatory_blank }
end
end
end

View file

@ -1172,14 +1172,14 @@ describe Dossier do
end
end
describe "#check_mandatory_champs" do
describe "#check_mandatory_and_visible_champs" do
include Logic
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:types_de_champ) { [type_de_champ] }
let(:type_de_champ) { {} }
let(:errors) { dossier.check_mandatory_champs }
let(:errors) { dossier.check_mandatory_and_visible_champs }
it 'no mandatory champs' do
expect(errors).to be_empty