Reputation: 110960
I have a Rails 3 form, with data-remote => true.
The form field is handle, like a twitter handle. I want to add validation to ensure it's handle friendly, meaning, no spaces, or non-url friendly characters.
Where to start, do I build a customer validation for this?
Thanks
Upvotes: 0
Views: 238
Reputation: 6168
In rails 3 the best practice will be using
validates :handle, :format => {:with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"}
Upvotes: 0
Reputation: 96934
Looks like you want to use validates_format_of
class Product < ActiveRecord::Base
validates_format_of :handle, :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"
end
Change the regular expression pattern to match your needs. See the Rails Guides on validation for more information.
Upvotes: 3
Reputation: 4478
Look into validates_each
validates_each :handle do |record,attribute,value|
# your validation code here, and you can record.errors.add().
end
Upvotes: 1