Merge pull request #7669 from betagouv/fix_hidden_required_in_conditionnel

fix(conditionnel): corrige les champs obligatoires mais cachés
This commit is contained in:
LeSim 2022-08-09 14:43:12 +02:00 committed by GitHub
commit 3eb440b9da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 49 deletions

View file

@ -34,7 +34,6 @@ class Champ < ApplicationRecord
delegate :libelle, delegate :libelle,
:type_champ, :type_champ,
:procedure, :procedure,
:mandatory?,
:description, :description,
:drop_down_list_options, :drop_down_list_options,
:drop_down_other?, :drop_down_other?,
@ -92,8 +91,12 @@ class Champ < ApplicationRecord
@sections ||= dossier.sections_for(self) @sections ||= dossier.sections_for(self)
end end
def mandatory?
type_de_champ.mandatory? && visible?
end
def mandatory_blank_and_visible? def mandatory_blank_and_visible?
mandatory? && blank? && visible? mandatory? && blank?
end end
def blank? def blank?

View file

@ -39,7 +39,7 @@ class Champs::PieceJustificativeChamp < Champ
end end
def mandatory_blank_and_visible? def mandatory_blank_and_visible?
mandatory? && !piece_justificative_file.attached? && visible? mandatory? && !piece_justificative_file.attached?
end end
def for_export def for_export

View file

@ -25,6 +25,6 @@ class Champs::SiretChamp < Champ
end end
def mandatory_blank_and_visible? def mandatory_blank_and_visible?
mandatory? && Siret.new(siret: value).invalid? && visible? mandatory? && Siret.new(siret: value).invalid?
end end
end end

View file

@ -33,7 +33,7 @@ class Champs::TitreIdentiteChamp < Champ
end end
def mandatory_blank_and_visible? def mandatory_blank_and_visible?
mandatory? && !piece_justificative_file.attached? && visible? mandatory? && !piece_justificative_file.attached?
end end
def for_export def for_export

View file

@ -1,5 +1,5 @@
#{champ.libelle} #{champ.libelle}
- if champ.mandatory? - if champ.type_de_champ.mandatory?
%span.mandatory * %span.mandatory *
- if champ.updated_at.present? && seen_at.present? - if champ.updated_at.present? && seen_at.present?

View file

@ -7,7 +7,7 @@
.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
= champ.drop_down_secondary_libelle.presence || "Valeur secondaire dépendant de la première" = champ.drop_down_secondary_libelle.presence || "Valeur secondaire dépendant de la première"
- if champ.mandatory? - if champ.type_de_champ.mandatory?
%span.mandatory * %span.mandatory *
- if champ.drop_down_secondary_description.present? - if champ.drop_down_secondary_description.present?
.notice{ id: "#{champ.describedby_id}-secondary" }= string_to_html(champ.drop_down_secondary_description) .notice{ id: "#{champ.describedby_id}-secondary" }= string_to_html(champ.drop_down_secondary_description)

View file

@ -269,65 +269,104 @@ describe 'The user' do
context 'with condition' do context 'with condition' do
include Logic include Logic
let(:procedure) do context 'with a required conditionnal champ' do
procedure = create(:procedure, :for_individual).tap do |p| let(:procedure) do
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'age') procedure = create(:procedure, :published, :for_individual,
p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'permis de conduire') types_de_champ_public: [
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'tonnage') { type: :integer_number, libelle: 'age' },
p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'parking') { type: :text, libelle: 'nom', mandatory: true }
])
age, nom = procedure.draft_revision.types_de_champ.all
nom.update(condition: greater_than_eq(champ_value(age.stable_id), constant(18)))
procedure
end end
age, permis, tonnage, parking = procedure.draft_revision.types_de_champ.all scenario 'submit a dossier with an hidden mandatory champ ', js: true do
log_in(user, procedure)
permis.update(condition: greater_than_eq(champ_value(age.stable_id), constant(18))) fill_individual
tonnage.update(condition: ds_eq(champ_value(permis.stable_id), constant(true)))
parking.update(condition: less_than_eq(champ_value(tonnage.stable_id), constant(20)))
procedure.publish! click_on 'Déposer le dossier'
expect(page).to have_current_path(merci_dossier_path(user_dossier))
end
procedure scenario 'cannot submit a reveal dossier with a revealed mandatory champ ', js: true do
log_in(user, procedure)
fill_individual
fill_in('age', with: '18')
expect(page).to have_css('label', text: 'nom *', visible: :visible)
click_on 'Déposer le dossier'
expect(page).to have_current_path(brouillon_dossier_path(user_dossier))
end
end end
scenario 'fill a dossier', js: true do context 'with a visibilite in cascade' do
log_in(user, procedure) let(:procedure) do
procedure = create(:procedure, :for_individual).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'age')
p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'permis de conduire')
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'tonnage')
p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'parking')
end
fill_individual age, permis, tonnage, parking = procedure.draft_revision.types_de_champ.all
expect(page).to have_css('label', text: 'age', visible: true) permis.update(condition: greater_than_eq(champ_value(age.stable_id), constant(18)))
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true) tonnage.update(condition: ds_eq(champ_value(permis.stable_id), constant(true)))
expect(page).to have_no_css('label', text: 'tonnage', visible: true) parking.update(condition: less_than_eq(champ_value(tonnage.stable_id), constant(20)))
fill_in('age', with: '18') procedure.publish!
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
choose('Oui') procedure
expect(page).to have_css('label', text: 'permis de conduire', visible: true) end
expect(page).to have_css('label', text: 'tonnage', visible: true)
fill_in('tonnage', with: '1') scenario 'fill a dossier', js: true do
expect(page).to have_css('label', text: 'parking', visible: true) log_in(user, procedure)
# try to fill with invalid data fill_individual
fill_in('tonnage', with: 'a')
expect(page).to have_no_css('label', text: 'parking', visible: true)
fill_in('age', with: '2') expect(page).to have_css('label', text: 'age', visible: true)
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true) expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true) expect(page).to have_no_css('label', text: 'tonnage', visible: true)
click_on 'Déposer le dossier' fill_in('age', with: '18')
click_on 'Accéder à votre dossier' expect(page).to have_css('label', text: 'permis de conduire', visible: true)
click_on 'Modifier mon dossier' expect(page).to have_no_css('label', text: 'tonnage', visible: true)
expect(page).to have_css('label', text: 'age', visible: true) choose('Oui')
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true) expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true) expect(page).to have_css('label', text: 'tonnage', visible: true)
fill_in('age', with: '18') fill_in('tonnage', with: '1')
# the champ keeps their previous value so they are all displayed expect(page).to have_css('label', text: 'parking', visible: true)
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_css('label', text: 'tonnage', visible: true) # try to fill with invalid data
fill_in('tonnage', with: 'a')
expect(page).to have_no_css('label', text: 'parking', visible: true)
fill_in('age', with: '2')
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
click_on 'Déposer le dossier'
click_on 'Accéder à votre dossier'
click_on 'Modifier mon dossier'
expect(page).to have_css('label', text: 'age', visible: true)
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
fill_in('age', with: '18')
# the champ keeps their previous value so they are all displayed
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_css('label', text: 'tonnage', visible: true)
end
end end
end end