diff --git a/app/models/champs/phone_champ.rb b/app/models/champs/phone_champ.rb index 8f89f159d..69099d145 100644 --- a/app/models/champs/phone_champ.rb +++ b/app/models/champs/phone_champ.rb @@ -15,4 +15,10 @@ # type_de_champ_id :integer # class Champs::PhoneChamp < Champs::TextChamp + validates :value, + phone: { + possible: true, + allow_blank: true, + message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages') + } end diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 925f958fd..62f2abb4f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -129,6 +129,7 @@ fr: blank: "doit être rempli" not_a_number: 'doit être un nombre' not_an_integer: 'doit être un nombre entier (sans chiffres après la virgule)' + not_a_phone: 'Numéro de téléphone invalide' greater_than: "doit être supérieur à %{count}" greater_than_or_equal_to: "doit être supérieur ou égal à %{count}" less_than: "doit être inférieur à %{count}" diff --git a/spec/features/users/brouillon_spec.rb b/spec/features/users/brouillon_spec.rb index e7bc4840b..2f17858a3 100644 --- a/spec/features/users/brouillon_spec.rb +++ b/spec/features/users/brouillon_spec.rb @@ -19,7 +19,7 @@ feature 'The user' do check('checkbox') choose('Madame') fill_in('email', with: 'loulou@yopmail.com') - fill_in('phone', with: '1234567890') + fill_in('phone', with: '0123456789') choose('Non') choose('val2') check('val1') @@ -55,7 +55,7 @@ feature 'The user' do expect(champ_value_for('checkbox')).to eq('on') expect(champ_value_for('civilite')).to eq('Mme') expect(champ_value_for('email')).to eq('loulou@yopmail.com') - expect(champ_value_for('phone')).to eq('1234567890') + expect(champ_value_for('phone')).to eq('0123456789') expect(champ_value_for('yes_no')).to eq('false') expect(champ_value_for('simple_drop_down_list')).to eq('val2') expect(champ_value_for('simple_choice_drop_down_list_long')).to eq('bravo') @@ -79,7 +79,7 @@ feature 'The user' do expect(page).to have_checked_field('checkbox') expect(page).to have_checked_field('Madame') expect(page).to have_field('email', with: 'loulou@yopmail.com') - expect(page).to have_field('phone', with: '1234567890') + expect(page).to have_field('phone', with: '0123456789') expect(page).to have_checked_field('Non') expect(page).to have_checked_field('val2') expect(page).to have_checked_field('val1') diff --git a/spec/models/champs/phone_champ_spec.rb b/spec/models/champs/phone_champ_spec.rb new file mode 100644 index 000000000..0da741ab9 --- /dev/null +++ b/spec/models/champs/phone_champ_spec.rb @@ -0,0 +1,25 @@ +describe Champs::PhoneChamp do + describe '#valid?' do + it do + expect(build(:champ_phone, value: nil)).to be_valid + expect(build(:champ_phone, value: "0123456789 0123456789")).to_not be_valid + expect(build(:champ_phone, value: "01.23.45.67.89 01.23.45.67.89")).to_not be_valid + expect(build(:champ_phone, value: "3646")).to be_valid + expect(build(:champ_phone, value: "0123456789")).to be_valid + expect(build(:champ_phone, value: "01.23.45.67.89")).to be_valid + expect(build(:champ_phone, value: "0123 45.67.89")).to be_valid + expect(build(:champ_phone, value: "0033 123-456-789")).to be_valid + expect(build(:champ_phone, value: "0033 123-456-789")).to be_valid + expect(build(:champ_phone, value: "0033(0)123456789")).to be_valid + expect(build(:champ_phone, value: "+33-1.23.45.67.89")).to be_valid + expect(build(:champ_phone, value: "+33 - 123 456 789")).to be_valid + expect(build(:champ_phone, value: "+33(0) 123 456 789")).to be_valid + expect(build(:champ_phone, value: "+33 (0)123 45 67 89")).to be_valid + expect(build(:champ_phone, value: "+33 (0)1 2345-6789")).to be_valid + expect(build(:champ_phone, value: "+33(0) - 123456789")).to be_valid + expect(build(:champ_phone, value: "+1(0) - 123456789")).to be_valid + expect(build(:champ_phone, value: "+49 2109 87654321")).to be_valid + expect(build(:champ_phone, value: "012345678")).to be_valid + end + end +end