Merge pull request #5490 from betagouv/5416-valid-phone
renforce la validation des numéros de téléphone
This commit is contained in:
commit
2df7408547
5 changed files with 38 additions and 22 deletions
|
@ -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
|
||||
|
|
|
@ -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]+"
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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')
|
||||
|
|
25
spec/models/champs/phone_champ_spec.rb
Normal file
25
spec/models/champs/phone_champ_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue