Chcę zwalidować numeryczność i długość dwóch rodzajów telefonów (komórka i stacjonarny). Oczywiście zezwalam, żeby pole mogło być puste (allow_nil):
9 validates_numericality_of :telefon, :allow_nil => true, :only_integer => true
10 validates_length_of :telefon, :is => 11, :allow_nil => true
11 validates_numericality_of :telefon_stacjonarny, :allow_nil => true, :only_integer => true
12 validates_length_of :telefon_stacjonarny, :is => 9, :allow_nil => true
W formularzu pozostawiam pola ‘telefon’ i ‘telefon_stacjonarny’ puste:
83 def validate
84 puts telefon #=>
85 puts telefon.class #=> String
86 puts telefon.size #=> 0
87 puts telefon_stacjonarny #=>
88 puts telefon_stacjonarny.class #=> String
89 puts telefon_stacjonarny.size #=> 0
90 puts telefon == telefon_stacjonarny #=> true
91 puts telefon === telefon_stacjonarny #=> true
92 end
A mimo to dostaję następujące błędy:
4 errors prohibited this user from being saved
There were problems with the following fields:
*Telefon is not a number
* Telefon is the wrong length (should be 11 characters)
* Telefon stacjonarny is not a number
* Telefon stacjonarny is the wrong length (should be 9 characters)
W czym może tkwić problem?