champs: put champ label in numeric fields validation messages
Replaces > La valeur du champ doit être un nombre entier (sans chiffres après > la virgule) by > La valeur du champ « Nombre de parents » doit être un nombre entier > (sans chiffres après la virgule)
This commit is contained in:
parent
56c846900b
commit
10065df8ce
4 changed files with 20 additions and 4 deletions
|
@ -1,5 +1,11 @@
|
||||||
class Champs::DecimalNumberChamp < Champ
|
class Champs::DecimalNumberChamp < Champ
|
||||||
validates :value, numericality: { allow_nil: true, allow_blank: true }
|
validates :value, numericality: {
|
||||||
|
allow_nil: true,
|
||||||
|
allow_blank: true,
|
||||||
|
message: -> (object, data) {
|
||||||
|
"« #{object.libelle} » " + object.errors.generate_message(data[:attribute].downcase, :not_a_number)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def for_export
|
def for_export
|
||||||
processed_value
|
processed_value
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
class Champs::IntegerNumberChamp < Champ
|
class Champs::IntegerNumberChamp < Champ
|
||||||
validates :value, numericality: { only_integer: true, allow_nil: true, allow_blank: true }
|
validates :value, numericality: {
|
||||||
|
only_integer: true,
|
||||||
|
allow_nil: true,
|
||||||
|
allow_blank: true,
|
||||||
|
message: -> (object, data) {
|
||||||
|
"« #{object.libelle} » " + object.errors.generate_message(data[:attribute].downcase, :not_an_integer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def for_export
|
def for_export
|
||||||
processed_value
|
processed_value
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Champs::DecimalNumberChamp do
|
describe Champs::DecimalNumberChamp do
|
||||||
subject { Champs::DecimalNumberChamp.new(value: value) }
|
subject { build(:champ_decimal_number, value: value).tap(&:valid?) }
|
||||||
|
|
||||||
describe '#valid?' do
|
describe '#valid?' do
|
||||||
context 'when the value is integer number' do
|
context 'when the value is integer number' do
|
||||||
|
@ -20,6 +20,7 @@ describe Champs::DecimalNumberChamp do
|
||||||
let(:value) { 'toto' }
|
let(:value) { 'toto' }
|
||||||
|
|
||||||
it { is_expected.to_not be_valid }
|
it { is_expected.to_not be_valid }
|
||||||
|
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre"]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the value is blank' do
|
context 'when the value is blank' do
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Champs::IntegerNumberChamp do
|
describe Champs::IntegerNumberChamp do
|
||||||
subject { Champs::IntegerNumberChamp.new(value: value) }
|
subject { build(:champ_integer_number, value: value).tap(&:valid?) }
|
||||||
|
|
||||||
describe '#valid?' do
|
describe '#valid?' do
|
||||||
context 'when the value is integer number' do
|
context 'when the value is integer number' do
|
||||||
|
@ -14,12 +14,14 @@ describe Champs::IntegerNumberChamp do
|
||||||
let(:value) { 2.6 }
|
let(:value) { 2.6 }
|
||||||
|
|
||||||
it { is_expected.to_not be_valid }
|
it { is_expected.to_not be_valid }
|
||||||
|
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the value is not a number' do
|
context 'when the value is not a number' do
|
||||||
let(:value) { 'toto' }
|
let(:value) { 'toto' }
|
||||||
|
|
||||||
it { is_expected.to_not be_valid }
|
it { is_expected.to_not be_valid }
|
||||||
|
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the value is blank' do
|
context 'when the value is blank' do
|
||||||
|
|
Loading…
Add table
Reference in a new issue