The Disco Blog

Can you dig it?

Lickety-split Custom Validations in Rails

Have a highly specific, yet custom validation for a particular field on one of your Rails model objects? Don’t want to create a ActiveModel::Validator type? Not a problem!

You can just as easily create a method that can be invoked as part of the validation process. For example, imagine a field dubbed uri in some model object; this field must begin with a protocol (i.e. http or https). You can create a validation method like so:

Custom validator
1
2
3
4
5
def uri_should_start_with_protocol
  if !uri.start_with?('http://') && !uri.start_with?('https://')
    errors.add(:uri, 'Web Address should start with http:// or https://')
  end
end

This method resides in your model. You can then register this method as a validation for your model like so:

Wiring the validation
1
validate :uri_should_start_with_protocol

Now if the uri field doesn’t contain http or https, model.save will return false. Done!

Comments