I have money-rails gem installed and it's working flawlessly. However, there are no validations on money objects when I create a new model record. If I try to input letters into the money field, the form submission is successful, it just sets the value to 0. How do I get those validations working? I have no code for the actual validation, seeing as money-rails on github states that it has validations included, but I have tried validates_numericality_of to no avail.
EDIT
Yes I have read the docs extensively, and tried the validation option suggested on Github.
I have this example hope it could help you
monetize :price_cents, :numericality => {:greater_than => 0, :less_than => 10}
validates :price, :format => { :with => /\A\d+(?:\.\d{0,2})?\z/ }
just like this one of them (monetize validation) will validate the numericality stuff and the other one will validate the format.
Related
hello I am building a rails application for the college community, the idea is that only students with a valid #college.edu email address can sign up for it.
There is a students table with college as its column
I have looked at the rails doc under validation, mostly it tells you how to validate length, presence, emptiness etc.
will this gem help me?
gem "validates_email_format_of", "~> 1.5.3"
I was reading up on email validation and it go into parsers, RFC 2822 and RFC 3696 ? is there a simpler way to go about it like regular expressions?
I'm not familiar with the validates_email_format_of gem, but the following example of the use of validates is documented in http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates
validates :email, :format => { :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create }
I'm adding validations to a model with a start column that is typed as datetime
Currently I only use the date_select form helper, but have left the column typed to datetime in case I decide I want to use the time value in the future.
I am currently using:
validates :start, :presence => true
But I want to know if there is a :format => that will ensure I'm getting a date passed in. I know it's unlikely that someone would change the select boxes around, but I figure you can't be too careful, right?
It depends what date formats you want to accept.
f.e.
validates :start, :presence => true, :format => /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/
for dd-mm-yyyy as dateformat.
Google for date regexp!
Since my answer was downvoted i give it another try:
validate do
self.errors[:start] << "must be a valid date" unless (DateTime.parse(self.start) rescue false)
end
You may want to check out validates_timeliness gem.
This gem enables some useful date and time-relations validations. In your case, you might use it to validate your start attribute like this:
class Task < ActiveRecord::Base
validates_datetime :start
# or
validates :start, presence: true, timeliness: {type: :datetime}
end
I have a model which validates presence of an attribute if a check box is checked in the view.
The code is something like this:
validates_presence_of :shipping_first_name, :if => :receive_by_email_is_unchecked
I am looking to have another condition of this validation.So how do I go about doing this ??
My assumption is that something like this would do:
validates_presence_of :shipping_first_name, :if => {:receive_by_email_is_unchecked,:form_first_step_validation}
I am not sure if this is the write way of doing it or not ??
Any suggestions would be appreciated.
You can pass method names in an array:
validates_presence_of :shipping_first_name, :if => [:receive_by_email_is_unchecked, :form_first_step_validation]
Alternatively you can use proc if you don't want to define separate methods just for conditioning validations:
validates_presence_of :shipping_first_name, :if => proc { !receive_by_email? && form_first_step_validation }
I don't think that will work, but have a look at the source code for validates_presence_of https://github.com/rails/rails/blob/master/activemodel/lib/active_model/validations/presence.rb
You can build your own validator to do exactly that
Ryan Bates covered this in one of his first Rails casts
http://railscasts.com/episodes/41-conditional-validations
It's still valid although syntax may be slightly different for Rails v 3 +
I assume you are working on a Rails 2.x app as the syntax you use is not Rails 3 syntax
Rails 3.x syntax would be
validates :field_1, :field_2, :presence_of => true, :if => # Use a proc, or an array of conditions here. see the valid examples and comments that you have already received for this question from #jimworm and #MichaĆ Szajbe
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 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