In my rails model I have some kind of template system. I want to make sure that users editing it do not make accidental mistakes so I use some simple validators.
They can use markers like ##user_id## that will be replaced later. I want to make sure they do not enter something like ###user_id## that contains too many #, so not any ### (or ####) must appear in the field.
class Template
validates_format_of :text, :with => /##user_id##/,
:message => "##user_id## must be present"
validates_format_of :text, :not_with => /###/,
:message => "Too many #"
end
Unfortunately there is no :not_with option ... is there any chance to solve it using a :with-regex or should I go a separate validate method?
I tried some negative look-ahead, but as there are (mostly) several ## and only one ### they always match one of them.
Use the :without option:
validates_format_of :text, :without => /###/, :message => "Too many #"
What about this...
validates_format_of :text, :with => /(^|[^#])##user_id##($|[^#])/
EDIT: I copied acheong87's rubular examples with my regex.
Can you do something like this?
/^(.(?!###+user_id##|##user_id###+))*$/
Here's a live demo: http://rubular.com/r/SPwsyDlj0y.
In (more) English, it says,
A string in which no character is followed by ###+user_id## or ##user_id###+.
Related
How can perform a reguler expression to validate for either - or _ in the person username. i dont want to accept any other character like .#()$etc just - or _ so the person can either have a name like mike, mikel_mark or mike-mark. very simple. Thank you
example:
validate_format_of :username, with: "...."
The Rails 3 way to do validations is the following:
validates :username, :format => {:with => /\A[0-9a-z_]+\Z/i}
The form of validate_format_of is more Rails < 3 like and followed the "type of validation" concept, whereas the validates form is attribute based (you write all validations that apply to the attribute in one statement).
Check out the docs here: http://apidock.com/rails/v3.2.13/ActiveModel/Validations/ClassMethods/validates
I have a field in my form, that should not accept some specific words(www, ftp, smtp, etc). Is there any validator that could make some kind of black listed words, that can not be written to db?
validates :subdomain, :exclusion => { :in => %w(www ftp smtp) }
ref: rails guide
You should create your own black list validator.
The syntax could be
validates :field, :black_list => {:file_path => "/path/to/words_file"}
Your validator will look to each word in the /path/to/words_file file and add errors on your model if the attribute field contains one black listed word.
How do you validate a U.S. zip code using Rails?
I wrote something like this but it doesn't work:
validates_format_of :zip_code,
:with => /^\d{5}(-\d{4})?$/,
:message => "Zip code should be valid"
You can also validate that it is actually a valid zip (not just the format but the zip itself) using:
http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP
Try a valid zip you know, e.g. 02135 vs an invalid one like 09990 to see the difference.
I would consider combining this with:
validates_format_of :zip, :with => /^\d{5}(-\d{4})?$/, :message => "should be in the form 12345 or 12345-1234"
that it's done with validate_format_of, rather than validate_format_of_zip_code as that means it can also be used for phone numbers, etc that also fit a fixed, known, format (.e.g. U.S.).
Perhaps validate format first and give error if invalid, so handle it all within rails with standard flash message.
Then, if valid make the call to that server to validate actual zip itself.
The only downside to great server supplied validations like this is that they increase the dependency on other sites and services. So if the other site/service changes things or is unavailable, etc. there is an issue. This is another reason why doing the simpler validity check first is a great idea.
A full service solution would also check for time-out by the zip code service and if that happens, say 5 seconds and the format is ok probably best to accept value. Maybe set an 'unverified_zip' flag if possible!
This worked for me: (ruby-2.0.0-p247, rails 4.0.0)
validates_format_of :zip_code,
with: /\A\d{5}-\d{4}|\A\d{5}\z/,
message: "should be 12345 or 12345-1234",
allow_blank: true
If you need multi-country support, you can use validates_zipcode gem I released. It currently supports 159 countries zipcode formats and plays nice with Rails 3 & 4.
You can use it like this:
class Address < ActiveRecord::Base
validates_zipcode :zipcode
validates :zipcode, zipcode: true
validates :zipcode, zipcode: { country_code: :ru }
validates :zipcode, zipcode: { country_code_attribute: :my_zipcode }
end
ZIP codes in the US are either 5 digits or 5 digits plus 4 digits for the area code. Try the following:
validates_format_of :zip_code,
:with => %r{\d{5}(-\d{4})?},
:message => "should be 12345 or 12345-1234"
those are both good answers!
another idea is to create you own custom validation , which not only checks that the number of digits is correct, but also checks with a database in the background, that the zip-codes exist..
e.g. these Gems could help:
geokit , check here: Best Zip Code Plugin for Ruby
zip-code-info , http://rubygems.org/gems/zip-code-info
http://zipcodesearch.rubyforge.org/
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
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.
Look into validates_each
validates_each :handle do |record,attribute,value|
# your validation code here, and you can record.errors.add().
end
In rails 3 the best practice will be using
validates :handle, :format => {:with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"}
i want to put validation in ruby on rails that my username should not contain special characters like !##$%^*()_-+=/<>?:'";.
Please tell me how can i implement it in my code.
Using your model validates_format_of as suggested by #Voyta
I am adding a regular expression in my example though :
validates_format_of :username, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/
Use validates_format_of (see comments there for regex examples):
validates_format_of :username, :with => /\Ayour_regex_here\Z/i