validates phone champ server side

This commit is contained in:
Christophe Robillard 2020-07-29 15:45:25 +02:00
parent 7faae3a438
commit 090a247ba9
4 changed files with 35 additions and 3 deletions

View file

@ -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

View file

@ -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}"

View file

@ -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')

View 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