In my form validation of my model, I'm trying to say that if the params of a column called :virtual is false, then the :location field should validate for :presence => true.
My current code is:
validates :location, if :virtual => false, :presence => true
But that's giving me a syntax error. What's the correct way to format this?
Something like:
attr_accessor :virtual # sets up a "virtual attribute" called "virtual" to which you can read/write a value
# this step isn't necessary if you already have an attribute on the model called "virtual"
validates :location, :presence => true, :unless => :virtual?
The use of virtual? should check whether the attribute virtual is true or false. Using unless means this validation is only performed if virtual is false (or is a value that is considered false).
More detail on virtual attributes and validation: Rails: Using form fields that are unassociated with a model in validations
validates :location, presence: true, if: Proc.new { |p| p.virtual == false }
Related
Thank you for looking onto this. I have a rails 5 API application. I am using ActiveModel Validations to validate params.
I need to validate the keys of params. ie. all the keys are mandatory to keep the structure of request unique, but it can be blank(ie. the values)
I know the
validates :key presence: true
validation, but it is checking there is a value for that. As i said, it can have blank values.
I am using params.permit so that additional keys are not allowed
include ActiveModel::Validations
validates :key1, presence: true
def initialize
#key1 = "val"
#key2 = "val2"
params.permit(:key1,:key2)
end
I need to compel the user to do requests with all the parameters with blanks allowed
Thanks in advance
Hello you should add the allow_blank options to your model like this:
validates :key presence: true, :allow_blank => true
Hope it can help
From the doc you need to specify allow_blank like :
validates :key1, :presence => true, :uniqueness => { :allow_blank => true, :case_sensitive => false }
Hope this helps !
You can try like below:
validates :key, presence: true, allow_blank: true
allow_blank:
The :allow_blank option is similar to the :allow_nil option.This option will let validation pass if the attribute's value is blank?, like nil or an empty string for example.
Note: nil values will be allowed
Hi I have a job model which has an string attribute called category. In the front end, I have a form with a dropbox where a user can fill out the category attribute with the selected value from the list. This is good enough front end validation for me, but now how will I do backend validation for the model?
I have dont other validations in the past for example:
validates :name, :presence => true
But is there anyway I can do something like
validates :category, :in => {"Food", "Drink", "Rental"}
You can do it like this:
validates :category, :inclusion => { :in => %w(Food Drink Rental) }
Or shorter:
validates :category, :inclusion => %w(Food Drink Rental)
Everything is in the documentation.
I can validate the phone_number for uniqueness like this in the model :
validates :phone_number, :uniqueness => true
The problem is that none of the users in the database have a phone number, and so they are all empty. While loading the page, without entring any phone number, I keep getting the following validation error :
Validation failed: Phone number has already been taken
Seems like it's even applying the validation for empty phone numbers.
How can I modify the validation to only apply to non-empty phone numbers in the params ?
Either this:
validates :phone_number, :uniqueness => true, :allow_nil => true
passes if phone_number is nil (or NULL in database), or this:
validates :phone_number, :uniqueness => true, :allow_blank => true
passes if phone_number is nil or empty string
Rails provides it out of the box. Check the docs: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of
validates_uniqueness_of :phone_number, :allow_blank => true
I am using Ruby on Rails 3 and I would like to retrieve validating field names. That is, I defined some validation for a class and I would like to retrieve what fields (their names) are candidate for validation on submitting a form.
I need that because I would like to "play" with class error attributes (<name_class>.errors).
How can I do?
You can access your model's validators method. This will return an array of validators on your model.
For example, if you had this:
class User < ActiveRecord::Base
validates :name, :presence => true
validates :email, :uniqueness => true
end
Then you could access the validators like this:
User.validators
# => [#<ActiveModel::Validations::PresenceValidator:0x123456 #attributes=[:name], #options={}>....]
User.validators.first.attributes
# => [:name]
User.validators.first.class
# => ActiveModel::Validations::PresenceValidator
I have a model with a whole bunch of fields. Not all fields are used based on the user selecting a certain type of form. I have around 6 different types of forms so a field may be used on 4 of them.
Is there a way to group validation based on a element ie?
case xxx
when "form1"
validates :field1, :presence => true
when "form2"
validates :field1, :presence => true
when "form3"
validates :fiel2, :presence => true
end
I will be doing client side validation but I obviously will need server side as well to make sure they have submitted good data.
Any suggestions how this can be done ?
I'm using Rails3 with Mongoid 2.0
Thanks in advance!
Something like this?
validates :field1, :presence => true, :if => Proc.new { |foo| %w{form1 form2}.include?(foo.xxx) }
validates :field2, :presence => true, :if => Proc.new { |foo| %w{form1 form3}.include?(foo.xxx) }
validates :field3, :presence => true, :if => Proc.new { |foo| %w{form2 form3}.include?(foo.xxx) }
I see a problem with the model class having to have intimate knowledge of the views involved. If the forms in the views were named differently, the solution won't work. You will want to use "validation groups" like that used in ASP.NET. You could do some search on that and either find a similar solution for Rails or roll your own. Maybe this one will help: https://github.com/akira/validationgroup