How do I use an if statement in Rails validation? - ruby-on-rails

I'm trying to create a validation statement that validates an object if service_area is present UNLESS service_area_radius==0
Here's the statement I created, which doesn't work:
validates :service_area, :presence => true, unless: "service_area_radius==0"

http://railscasts.com/episodes/41-conditional-validations
Like this:
validates_presence_of :password, :if => :should_validate_password?
validates_presence_of :country
validates_presence_of :state, :if => :in_us?
attr_accessor :updating_password
def in_us?
country == 'US'
end
def should_validate_password?
updating_password || new_record?
end

validates :service_area,
presence: {message: "Area Radius is missing."}, if: :radius_found?
private
def radius_found?
service_area_radius > 0
end
The validation for service_area will be executed if radius_found? returns true.
radius_found? will return true when the service_area_radius(attribute) hold value > 0.
Adding a custom message with message: option, when the validation fails.

Related

Validate uniquiness before save, with condition [duplicate]

On Rails 5.
I have an Order model with a description attribute. I only want to validate it's presence if one of two conditions is met: if the current step is equal to the first step OR if require_validation is equal to true.
I can easily validate based on one condition like this:
validates :description, presence: true, if: :first_step?
def first_step?
current_step == steps.first
end
but I am not sure how to go about adding another condition and validating if one or the other is true.
something like:
validates :description, presence: true, if: :first_step? || :require_validation
Thanks!
You can use a lambda for the if: clause and do an or condition.
validates :description, presence: true, if: -> {current_step == steps.first || require_validation}
Can you just wrap it in one method? According to the docs
:if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. if: :allow_validation, or if: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
validates :description, presence: true, if: :some_validation_check
def some_validation_check
first_step? || require_validation
end
You can pass a lambda to be evaluated as the if condition.
Try:
validates :description, presence: true, if: -> { first_step? || require_validation }
If you don't want to add one method as Jared say then you can try use lambda
validates :description, presence: true, if: ->{ first_step? || require_validation }
If you have a lot case , you can design for validates
validates_presence_of :price_tech_fee, if: :price_tech_fee_require?, :message => :required
validates_presence_of :percentage_tech_fee, if: :percentage_tech_fee_require?, :message => :required
def percentage_tech_fee_require?
is_active? && is_transaction_percentage? && is_premium?
end
def is_active?
!self.is_deleted && self.is_active
end
def is_transaction_percentage?
self.is_per_transaction && self.is_percentage
end
def is_premium?
....
end

Rails - Validation :if one condition is true

On Rails 5.
I have an Order model with a description attribute. I only want to validate it's presence if one of two conditions is met: if the current step is equal to the first step OR if require_validation is equal to true.
I can easily validate based on one condition like this:
validates :description, presence: true, if: :first_step?
def first_step?
current_step == steps.first
end
but I am not sure how to go about adding another condition and validating if one or the other is true.
something like:
validates :description, presence: true, if: :first_step? || :require_validation
Thanks!
You can use a lambda for the if: clause and do an or condition.
validates :description, presence: true, if: -> {current_step == steps.first || require_validation}
Can you just wrap it in one method? According to the docs
:if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. if: :allow_validation, or if: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
validates :description, presence: true, if: :some_validation_check
def some_validation_check
first_step? || require_validation
end
You can pass a lambda to be evaluated as the if condition.
Try:
validates :description, presence: true, if: -> { first_step? || require_validation }
If you don't want to add one method as Jared say then you can try use lambda
validates :description, presence: true, if: ->{ first_step? || require_validation }
If you have a lot case , you can design for validates
validates_presence_of :price_tech_fee, if: :price_tech_fee_require?, :message => :required
validates_presence_of :percentage_tech_fee, if: :percentage_tech_fee_require?, :message => :required
def percentage_tech_fee_require?
is_active? && is_transaction_percentage? && is_premium?
end
def is_active?
!self.is_deleted && self.is_active
end
def is_transaction_percentage?
self.is_per_transaction && self.is_percentage
end
def is_premium?
....
end

How to skip multiple validation ruby on rails

I have 2 forms.
Form 1 I have 10 fields which I am validating.
Form 2 but it only contains 2 fields.
The model is same.
What I need to do is:
To validate fields when they are submitted by forms. If I am not posting any fields it should not validate in model. If I post 5 fields it should validate 5 fields. If I post 2 fields it should validate only 2 not all of them.
So form 1 all 10 should be validated, form 2 only 2 should validate not rest of 8.
Here is my code:
validates :teacher_number, :title, :name, :gender, :location, :dob,
:contact_mobile, :contact_home, :street, :city, :state, :zip_code, :country,
presence: true
validates :teacher_number, uniqueness: {scope: :school_id}
validate :teacher_number_existance, :on => :create
validate :school_existance, :on => :create
Below is my attempt which successfully works fine but its bulk of code that somewhat a bad practice.
validates :teacher_number, presence: true, if: "teacher_number && teacher_number.blank?"
validates :title, presence: true, if: "title && title.blank?"
validates :name, presence: true, if: "name && name.blank?"
validates :gender, presence: true, if: "gender && gender.blank?"
validates :location, presence: true, if: "location && location.blank?"
validates :dob, presence: true, if: "dob && dob.blank?"
validates :contact_mobile, presence: true, if: "contact_mobile && contact_mobile.blank?"
validates :contact_home, presence: true, if: "contact_home && contact_home.blank?"
validates :street, presence: true, if: "street && street.blank?"
validates :city, presence: true, if: "city && city.blank?"
validates :state, presence: true, if: "state && state.blank?"
validates :zip_code, presence: true, if: "zip_code && zip_code.blank?"
validates :country, presence: true, if: "country && country.blank?"
validates :teacher_number, uniqueness: {scope: :school_id}, if: "teacher_number && teacher_number.blank?"
validate :teacher_number_existance, :on => :create, if: "self.teacher_number && self.teacher_number.blank?"
validate :school_existance, :on => :create, if: "self.teacher_number && self.teacher_number.blank?"
EDIT
UPDATED MY QUESTION.
I see two ways for this:
Some hidden param in form and attr_accesor in model to turn off validation.
Use save(validate: false) for save from that second form.
Next, you can do it like this
if validate_object?
validates :email, presence: true
validates :variant, presence: true
end
You can use some patterns like form object.
But you have to remember that this object will be invalid in future too.
If you want different validations of data in different circumstances, you should not be validating on the model. You should validate elsewhere: either in the view using HTML form validation, or in the controller using Rails' Strong Params.
I think you should use HTML form validation to manage this, because you're worried about the record missing attributes. Strong Params is more useful in case you are worried about people supplying forbidden values for record attributes.
Here is how you would use HTML form validation to manage this (example using HAML):
= form_for #model do |f|
= f.text_input(:teacher_number, required: true)
...all your other inputs...
= f.submit 'Submit Form'
Here is how you would use Strong Params to constrain the number of things you can get:
class ModelsController < ApplicationController
def form_action_1
Model.create(form_1_params)
end
def form_action_2
Model.create(form_2_params)
end
private
def form_1_params
# let's permit all the things you want for the first form
params.require(:model).permit(:teacher_number, :title, ...)
end
def form_2_params
# let's permit only the few things you want in the second form
params.require(:model).permit(:only, :a, :few, :things)
end
end
I'm not sure exactly what you're asking, but perhaps this approach would work. Write your validations like this:
validates :teacher_number,
:title,
:name,
:gender,
:location,
:dob,
:contact_mobile,
:contact_home,
:street,
:city,
:state,
:zip_code,
:country,
presence: true,
on: :form2
validates :teacher_number, uniqueness: {scope: :school_id}
validate :teacher_number_existance, :on => :create
validate :school_existance, :on => :create
The on: :form2 near the bottom of the first validator means the validations will only run if you explicitly ask them to run.
So in your controller action for saving form2, you would have to do this:
if valid?(:form2) && #model.save
The other validators will run when you save, as normal. Using the on: parameter of validates for your own symbols (as opposed to the built-in ones) is covered in the Rails Guide for validations, but it's easy to miss. I didn't notice it myself until recently. I hope this helps.
Finally, after so going back and forth below solution worked well for me.
before_validation :strip_validations
def strip_validations
['teacher_number', 'title', 'name', 'gender', 'location', 'dob', 'contact_mobile', 'contact_home', 'street', 'city', 'state', 'zip_code', 'country'].each do |attr|
errors.add("Teacher", {attr => " #{attr} can't be blank"}) if send(attr.to_sym) && send(attr.to_sym).blank?
end
end

How to use with_options for conditional validation

How can I use with_options for conditional validation ?
My code is
with_options if: (AppUser::User.creator=="is_admin") do |admin|
admin.validates :first_name, :presence => true
admin.validates :last_name, :presence => true
end
I have already set creator method in application controller.
before_action :set_global_user
def set_global_user
if current_admin
AppUser::User.creator= "is_admin"
elsif current_user
AppUser::User.creator= "is_user"
else
AppUser::User.creator=nil
end
end
but I am getting
undefined method `validate' for false:FalseClass
what is wrong with this code.
because
(AppUser::User.creator == "is_admin")`
does not return an object but it is a boolean.
Try this (inside your model):
class User < ActiveRecord::Base
with_options if: (AppUser::User.creator == "is_admin") do
validates :first_name, presence: true
validates :last_name, presence: true
end
end
P.S: I recommend the use of the devise gem to manage user types like this:
devise and multiple “user” models.

Strong parameters in Ruby

I'm getting the error message about strong parameters. I think it's just that rails 4 doesn't use attributes anymore. the code for my toy.rb is:
class Toy < ActiveRecord::Base
attr_accessible :name, :price, :vendor
validates :name, :presence => true
validates :price, :presence => true
validates :price, :numericality => true
validates :vendor, :presence => true
end
how can I change this to strong parameters?
EDIT: I used a different rb i changed it to employees and this is what I have:
class Employee < ActiveRecord::Base
params.require(:employee).permit(:first, :last, :salary, :salary, :ssn)
validates :first, :presence => true
validates :last, :presence => true
validates :salary, :presence => true
validates :salary, :numericality => true
validates :ssn, :presence => true
end
It's still telling me "ndefined local variable or method `params' for #"
The code you need is
params.require(:toy).permit(:name, :price, :vendor)
You will put this in your controller. Typically, you create a private method:
def create
Toy.create(toy_params)
end
private
def toy_params
params.require(:toy).permit(:name, :price, :vendor)
end
See http://guides.rubyonrails.org/getting_started.html#saving-data-in-the-controller for more information.
Edit
I think I might have misled you with my original answer. The code goes in the controller, not the model.
Strong params are designed to help your controller send specific data to your model. It's meant to protect your app against unauthorized data being passed:
#app/controllers/toys_controller.rb
Class ToysController < ActiveRecord::Base
def new
#toy = Toy.new #-> creates a blank AR object
end
def create
#toy = Toy.new(toys_params) #->creates new AR object (populating with strong params)
#toy.save
end
private
def toys_params
params.require(:toys).permit(:your, :params, :here)
end
end

Resources