Note that there are some explanatory texts on larger screens.

plurals
  1. POOnly validate an Rails3 Model when an value entered
    primarykey
    data
    text
    <p>I need to validate a filed called phone_number in my rails3 app. This filed is optional but when a user enter a phone_number i will check the format. The RSpec2 test runs fine but when i go to the sign_up view and don't touch the phone_number field i become an "Phone number is too short (minimum is 6 characters)" and "Phone number is invalid" error.</p> <p>What is the problem in my model? My target is to validate the phone_number if a user enter this if the number blank i will save an nil in my database.</p> <p>This is my User model:</p> <pre><code>class User &lt; ActiveRecord::Base belongs_to :address accepts_nested_attributes_for :address # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :confirmable, :lockable, :validatable # Setup accessible (or protected) attributes for your model attr_readonly :username attr_accessible :email, :password, :password_confirmation, :remember_me, :phone_number, :username, :newsletter, :address_attributes validates :username, :presence =&gt; true, :uniqueness =&gt; true, :length =&gt; {:minimum =&gt; 4, :maximum =&gt; 16 }, :format =&gt; { :with =&gt; /\A[a-z0-9][a-z0-9._-]*\z/i } validates :phone_number, :length =&gt; {:minimum =&gt; 6, :maximum =&gt; 25}, :format =&gt; { :with =&gt; /\A\S[0-9\+\/\(\)\s\-]*\z/i }, :allow_nil =&gt; true validates :address, :presence =&gt; true end </code></pre> <p>My Rspec Method to test the phone_number:</p> <pre><code>it "should be valid without an phonenumber" do Factory.build(:user, :phone_number =&gt; nil).should be_valid end it "should be invalid with an invalid phonenumber" do invalid_phonenumbers.each do |invalid| Factory.build(:user, :phone_number =&gt; invalid).should_not be_valid end end it "should be valid with an valid phonenumber" do valid_phonenumbers.each do |valid| Factory.build(:user, :phone_number =&gt; valid).should be_valid end end def invalid_phonenumbers ["Hans Wurst","+49 221 Hans","Gebe ich nicht ein"," ","110",""] end def valid_phonenumbers ["+492203969534","0221/549534","0800-2222 800","+49-0221-2222-390","+49 (221) / 549534 - 23","+49 (0) 221 / 549534 - 23","0221269534"] end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload