Rails Model Validations for Length and Numericality doesn't accept numbers starting with Zero -


i have field zip_code in table.i have provided validations field in corresponding model follows :

validates :zip_code, :length => { :minimum => 5, :maximum => 6 }, :numericality => true, allow_blank: true 

the field integer in table.when try input value '01234' doesnot accept , gives error this:

zip code short (minimum 5 characters) 

it helpful if can throw light problem.

you see, zip_code should not number. should string format of number. guys commented in question, need change zip_code column of string type. write new migration it:

rails g migration change_model_zip_code_type  

then in migration:

def change   change_column :my_table, :zip_code, :string end 

then type in console:

rake db:migrate 

now can validate zip code numerical string:

validates :zip_code, :length => { :minimum => 5, :maximum => 6 }, :format => { with: /[0-9]+/ } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -