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
Related
Is there a difference between using
validates :foo, uniqueness: true
or
validates_uniqueness_of :foo?
I know this is a simple questions, but Google didn't help
When and why should one be used over the other?
The validates method is a shortcut to all the default validators that Rails provides. So, validates :foo, uniqueness: true would trigger UniquenessValidator under the hood. The source code for validates can be found in the API doc here. As shown there, it basically triggers the validators of the options passed and raises an error in case an invalid option is passed.
validates_uniqueness_of also triggers the UniquenessValidator, the same as validates. Its source code is
# File activerecord/lib/active_record/validations/uniqueness.rb, line 233
def validates_uniqueness_of(*attr_names)
validates_with UniquenessValidator, _merge_attributes(attr_names)
end
The only difference is that with validates_uniqueness_of, we can ONLY validate the uniqueness and not pass additional options, whereas validates accepts multiple options. So we could have the following validations with validates:
validates :name, presence: true, uniqueness: true, <some other options>
But the same would not be possible with validates_uniqueness_of.
In my validations, I have already used once a conditional presence like
validates :contact_name, presence: true
validates :contact_adress,
presence: true, if: :contact_name?,
Now I would like to do the contrary: condition the obligation of an attribute to the absence of the other attribute. If I let A empty, then B has to be filled and vice versa.
Would that work?
validates :attribute_A, presence true, if:attribute_B!
validates :attribute_B, presence true, if:attribute_A!
You can use unless or .blank?
validates :attribute_A, presence true, if: :attribute_B.blank?
OR
validates :attribute_A, presence true, unless: :attribute_B
From documentation of validates
There is also a list of options that could be used along with validators:
...
:unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. unless: :skip_validation, or unless: Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.
However, it may be more clear to use if: with the help of Object#present? or Object#blank?.
In Ruby on Rails, I can't seem to find a validation model method to check for data-type.
I was hoping for something like
validates :name, datatype: :integer
but there is nothing on http://guides.rubyonrails.org/testing.html
If such a test not required? Is it because the database engine (mysql or whatever) itself would reject information with a wrong data-type?
It seems you are new to Rails there are already predefined validations in rails Ref Active Record Validations in Rails
validates :name, numericality: true
or
validates :name, numericality: { only_integer: true }
You can do something like
validates :your_field, :numericality => { :greater_than_or_equal_to => 0 }
please check the link
Validation for non-negative integers and decimal values
There is a validation for this in Rails
validates_numericality_of :value, :only_integer: true
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 }
I'm quite new to ruby/rails. I was wondering what is the best way to ensure that two people don't choose the same username. Here is my model at the moment:
class User < ActiveRecord::Base
validates :username, :presence => true
validates :password, :presence => true, :length => { :minimum => 7}
end
Note: I'm assuming it is best to place this type of code in the model. Correct me if I'm wrong.
There's a validation to make sure a field is unique. Just change your username validation to:
validates :username, :presence => true, :uniqueness => true
You should also add an index to your usertable, with uniqueness. This way, if people quickly press the username register button twice, you will also be protected at the database level
add_index :users, :username, :unique => true
This question has already been correctly answered but for future reference, APIDock has excellent Rails documentation here: http://apidock.com/rails. The search's autocomplete is fantastic.
The documentation for the validates method is here: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates where you can find the :uniqueness => true option.