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
Post a Comment