I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am us
I think it might need something like:
validates_length_of :foo, minimum: 5, maximum: 5, allow_blank: true
More examples: ActiveRecord::Validations::ClassMethods
every validates_* accepts :if or :unless options
validates_length_of :foo, :maximum => 5, :if => :validate_foo_condition
where validate_foo_condition is method that returns true or false
you can also pass a Proc object:
validates_length_of :foo, :maximum => 5, :unless => Proc.new {|object| object.foo.blank?}
Add in your model:
validates :color, length: { is: 7 }
color is a string:
t.string :color, null: false, default: '#0093FF', limit: 7