validate values inclusion
This commit is contained in:
parent
d648ac31c2
commit
d5ffd61ab6
9 changed files with 64 additions and 7 deletions
|
@ -23,6 +23,8 @@
|
|||
class Champs::MultipleDropDownListChamp < Champ
|
||||
before_save :format_before_save
|
||||
|
||||
validate :values_are_in_options, if: -> { value.present? }
|
||||
|
||||
def options?
|
||||
drop_down_list_options?
|
||||
end
|
||||
|
@ -90,4 +92,11 @@ class Champs::MultipleDropDownListChamp < Champ
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def values_are_in_options
|
||||
return if (json = JSON.parse(value) - ['']).empty?
|
||||
return if json.filter { |val| enabled_non_empty_options.exclude?(val) }.empty?
|
||||
|
||||
errors.add(:value, :not_in_options)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -42,6 +42,7 @@ class PrefillParams
|
|||
TypeDeChamp.type_champs.fetch(:pays),
|
||||
TypeDeChamp.type_champs.fetch(:regions),
|
||||
TypeDeChamp.type_champs.fetch(:departements),
|
||||
TypeDeChamp.type_champs.fetch(:multiple_drop_down_list),
|
||||
TypeDeChamp.type_champs.fetch(:epci)
|
||||
]
|
||||
|
||||
|
|
|
@ -471,6 +471,10 @@ en:
|
|||
attributes:
|
||||
value:
|
||||
not_in_options: "must be in the given options"
|
||||
"champs/multiple_drop_down_list_champ":
|
||||
attributes:
|
||||
value:
|
||||
not_in_options: "must be in the given options"
|
||||
"champs/region_champ":
|
||||
attributes:
|
||||
value:
|
||||
|
|
|
@ -466,6 +466,10 @@ fr:
|
|||
attributes:
|
||||
value:
|
||||
not_in_options: "doit être dans les options proposées"
|
||||
"champs/multiple_drop_down_list_champ":
|
||||
attributes:
|
||||
value:
|
||||
not_in_options: "doit être dans les options proposées"
|
||||
"champs/region_champ":
|
||||
attributes:
|
||||
value:
|
||||
|
|
|
@ -792,7 +792,7 @@ describe Instructeurs::DossiersController, type: :controller do
|
|||
champs_private_attributes: {
|
||||
'0': {
|
||||
id: champ_multiple_drop_down_list.id,
|
||||
value: ['', 'un', 'deux']
|
||||
value: ['', 'val1', 'val2']
|
||||
},
|
||||
'1': {
|
||||
id: champ_datetime.id,
|
||||
|
@ -813,7 +813,7 @@ describe Instructeurs::DossiersController, type: :controller do
|
|||
end
|
||||
|
||||
it {
|
||||
expect(champ_multiple_drop_down_list.value).to eq('["un", "deux"]')
|
||||
expect(champ_multiple_drop_down_list.value).to eq('["val1", "val2"]')
|
||||
expect(champ_linked_drop_down_list.primary_value).to eq('primary')
|
||||
expect(champ_linked_drop_down_list.secondary_value).to eq('secondary')
|
||||
expect(champ_datetime.value).to eq('2019-12-21T13:17:00+01:00')
|
||||
|
@ -839,7 +839,7 @@ describe Instructeurs::DossiersController, type: :controller do
|
|||
champs_public_attributes: {
|
||||
'0': {
|
||||
id: champ_multiple_drop_down_list.id,
|
||||
value: ['', 'un', 'deux']
|
||||
value: ['', 'val1', 'val2']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -97,7 +97,7 @@ FactoryBot.define do
|
|||
|
||||
factory :champ_multiple_drop_down_list, class: 'Champs::MultipleDropDownListChamp' do
|
||||
type_de_champ { association :type_de_champ_multiple_drop_down_list, procedure: dossier.procedure }
|
||||
value { '["choix 1", "choix 2"]' }
|
||||
value { '["val1", "val2"]' }
|
||||
end
|
||||
|
||||
factory :champ_linked_drop_down_list, class: 'Champs::LinkedDropDownListChamp' do
|
||||
|
|
|
@ -117,7 +117,7 @@ describe Champ do
|
|||
# when using the old form, and the ChampsService Class
|
||||
# TODO: to remove
|
||||
context 'when the value is already deserialized' do
|
||||
let(:value) { '["1", "2"]' }
|
||||
let(:value) { '["val1", "val2"]' }
|
||||
|
||||
it { expect(champ.value).to eq(value) }
|
||||
|
||||
|
@ -133,9 +133,9 @@ describe Champ do
|
|||
# GOTCHA
|
||||
context 'when the value is not already deserialized' do
|
||||
context 'when a choice is selected' do
|
||||
let(:value) { '["", "1", "2"]' }
|
||||
let(:value) { '["", "val1", "val2"]' }
|
||||
|
||||
it { expect(champ.value).to eq('["1", "2"]') }
|
||||
it { expect(champ.value).to eq('["val1", "val2"]') }
|
||||
end
|
||||
|
||||
context 'when all choices are removed' do
|
||||
|
|
38
spec/models/champs/multiple_drop_down_list_champ_spec.rb
Normal file
38
spec/models/champs/multiple_drop_down_list_champ_spec.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
describe Champs::MultipleDropDownListChamp do
|
||||
describe 'validations' do
|
||||
describe 'inclusion' do
|
||||
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_list_value: "val1\r\nval2\r\nval3") }
|
||||
subject { build(:champ_multiple_drop_down_list, type_de_champ:, value:) }
|
||||
|
||||
context 'when the value is nil' do
|
||||
let(:value) { nil }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context 'when the value is an empty string' do
|
||||
let(:value) { '' }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context 'when the value is an empty array' do
|
||||
let(:value) { [] }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context 'when the value is included in the option list' do
|
||||
let(:value) { ["val3", "val1"] }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context 'when the value is not included in the option list' do
|
||||
let(:value) { ["totoro", "val1"] }
|
||||
|
||||
it { is_expected.not_to be_valid }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -189,6 +189,7 @@ RSpec.describe PrefillParams do
|
|||
it_behaves_like "a champ public value that is unauthorized", :siret, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :rna, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :annuaire_education, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :multiple_drop_down_list, ["value"]
|
||||
end
|
||||
|
||||
private
|
||||
|
|
Loading…
Reference in a new issue