From 090a247ba91f393083e7fcf42a5ae0f0f394aeab Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 29 Jul 2020 15:45:25 +0200 Subject: [PATCH 1/2] validates phone champ server side --- app/models/champs/phone_champ.rb | 6 ++++++ config/locales/fr.yml | 1 + spec/features/users/brouillon_spec.rb | 6 +++--- spec/models/champs/phone_champ_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 spec/models/champs/phone_champ_spec.rb 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 From abe62398b6edca0ba23e589e86b94f243d5ecd08 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 19 Aug 2020 14:55:44 +0200 Subject: [PATCH 2/2] very light client-side validation for phone --- .../dossiers/editable_champs/_phone.html.haml | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/app/views/shared/dossiers/editable_champs/_phone.html.haml b/app/views/shared/dossiers/editable_champs/_phone.html.haml index 5ef3c3dfa..888cdb944 100644 --- a/app/views/shared/dossiers/editable_champs/_phone.html.haml +++ b/app/views/shared/dossiers/editable_champs/_phone.html.haml @@ -1,23 +1,7 @@ -# Allowed formats: --# 0123456789 --# 01 23 45 67 89 --# 01.23.45.67.89 --# 0123 45.67.89 --# 0033 123-456-789 --# 0035 123-456-789 --# 0033 123-456-789 --# 0033(0)123456789 --# +33-1.23.45.67.89 --# +33 - 123 456 789 --# +33(0) 123 456 789 --# +33 (0)123 45 67 89 --# +33 (0)1 2345-6789 --# +33(0) - 123456789 --# +1(0) - 123456789 --# +2 123456789 --# 012345678 --# 01234567890 +-# very light validation is made client-side +-# stronger validation is made server-side = form.phone_field :value, placeholder: champ.libelle, required: champ.mandatory?, - pattern: "([\\+\\d\\(][\\(\\)\\s\\.\\-\\d]{4,}\\d)" + pattern: "[^a-z^A-Z]+"