Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord validate url if it is present

I would like to ensure that my class's url property has a value and if it does, it is valid:

class Entity < ActiveRecord::Base

  validates :name, presence: true
  validates :url, presence: true, :format => {:with => URI.regexp}

end

In the rails console:

> e = Entity.new(name: 'foo')
=> #<Entity id: nil, name: "foo", url: nil, created_at: nil, updated_at: nil> 

Which results in two errors for the url attribute:

> e.valid?
=> false

> e.errors
=> #<ActiveModel::Errors:0x007fed9e324e28 @base=#<Entity id: nil, name: "foo", url: nil, created_at: nil, updated_at: nil>, @messages={:url=>["can't be blank", "is invalid"]}> 

Ideally, a nil url would produce a single error (i.e. can't be blank).

As such, I've change the validates rule:

validates :url, presence: true, :with => Proc.new { URI.regexp if :url? }

I can't get the syntax to work, however. What am I missing?

like image 610
craig Avatar asked Jul 09 '14 16:07

craig


People also ask

How do I check if a URL is valid?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

How does validation work in Rails?

Before saving an Active Record object, Rails runs your validations. If these validations produce any errors, Rails does not save the object. After Active Record has performed validations, any errors found can be accessed through the errors instance method, which returns a collection of errors.


2 Answers

Separate your two validators.

validates :url, presence: true
validates :url, format: { with: URI.regexp }, if: Proc.new { |a| a.url.present? }

(almost) 2 year anniversary edit

As vrybas and Barry state, the Proc is unnecessary. You can write your validators like this:

validates :url, presence: true
validates :url, format: { with: URI.regexp }, if: 'url.present?'
like image 99
Yanis Vieilly Avatar answered Sep 24 '22 19:09

Yanis Vieilly


Separate the validators as in Yanis's answer, but you don't need a Proc for this.

You can use the common validation options to bypass the format validation if the value is nil by setting the allow_nil parameter.

Alternatively, setting the allow_blank parameter would also work if the value is the empty string '', which may be more useful if you're setting url from an form input.

The complete validator could look like this:

validates :url, presence: true
validates :url, format: { with: URI.regexp }, allow_blank: true
like image 29
Barry Avatar answered Sep 26 '22 19:09

Barry