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.
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
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###+.
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"}
How to remove Field Name in validates_presence_of on rails Model for the following coding
validates_presence_of :address, :attributes => true, :discard_if => :invalid?, :on => :save, :message=> "Invalid Address"
and the Output is
Address Invalid address
and I don't want Address field in this validation
Please Help me to solve this problem
I believe this is the answer you're looking for:
Fully custom validation error message with Rails
The standard error format is "%{attribute} %{message}". If you don't want attribute names included in messages, you can change errors.format in your locale.
# config/locales/en.yml
en:
errors:
format: '%{message}'
AFAIK, you can't do this for a single attribute.
I was searching for the same question and stops on:
*instance.errors.add*
for exmpl,
question.errors.add("Answer","can't be blank") if self.body.blank?
There is *errors.add_to_base*, but just add is working more comfortably, for my opinion.
I have a Product model which validates multiple attributes (including a Paperclip image attachment) like so:
validates_presence_of :name
validates_format_of :name, :with => /^([a-zA-Z0-9\ \-]{3,128})$/i
...
has_attached_file :image
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ["image/jpeg", "image/png", "image/gif"]
Everything is working fine. What I want now is to make an (unobtrusive) hidden iframe in-place upload script using javascript. My problem is that I cannot just upload the image without the rest of the data, because it will fail validation (no name present) and also I cannot send the rest of the form without the image (same thing, fails validation).
So basically what I need (and don't know how to achieve) is to conditionally apply the model validations according to what the action is currently in progress (uploading the image or editing other data).
I hope I was clear enough. Any help is appreciated. Thanks.
Railscasts have a nice video screencast about conditional validations.
I hate having to wade through a video just to get a simple answer. In fact, I think that a blog entry is superior to a simple tutorial video simply for the fact that it is searchable. Here is a simple case in plain text for anyone else looking:
To validate the presence of password only for the create action, do this:
validates_presence_of :password, :on => :create
A comment for Peter D.
Thank you very much. I'm unable to watch that screen cast presently (though I plan to) and was looking for a speedy, brief answer.
Incorporated your suggestion into a model I have and it's working perfectly. (Though I suspect at some point I'm going to need validation on update when passwords are being changed. I'll take it as "technical debt" now though, in order to move on.)
The bit I added:
validates :password, :presence => true, :confirmation => true, :on => :create