Pass field name as parameter to custom validation method Rails 4 - ruby-on-rails

I have a custom validation method:
def my_custom_validation
errors.add(specific_field, "error message") if specific_field.delete_if { |i| i.blank? }.blank?
end
The goal is to disallow parameters which contains [""] pass through validation, but I need to call this method like:
validate :my_custom_validation #and somehow pass here my field names
For example:
validate :my_custom_validation(:industry)

Since you need to validate multiple attributes this way I would recommend a custom validator like so:
class EmptyArrayValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "cannot be emtpy") if value.delete_if(&:blank?).empty?
end
end
Then validate as
validates :industry, empty_array: true
validates :your_other_attribute, empty_array: true
Or if you don't want to specifically create a class because it is only needed for 1 model you could include this in the model itself
validates_each :industry, :your_other_attribute, :and_one_more do |record, attr, value|
record.errors.add(attr, "cannot be emtpy") if value.delete_if(&:blank?).empty?
end

If you'd like to keep the method based validation you can use a Ruby lambda like below:
validate -> { my_custom_validation(some_model_field) }
See similar question

Related

Checking if attribute_changed? in ActiveModel::EachValidator

I have a class Person that has first_name, middle_name, last_name.
I have a customed Each validator for these 3 attributes like so
validates :first_name, :middle_name, :last_name, nameValidator: true
In this validator I want to check if any one of these 3 attributes changed and given a few more conditions I'll validate name. For that I'm trying attribute_changed? but it doesn't work.
I've checked different methods from ActiveModel::Dirty and Activerecod::Dirty but nothing seems to work to check changes in each attribute. What am I missing?
module Person
class nameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless can_normalize?(record)
#Normalization code
end
def can_normalize?(record)
anything_new = record.new_record? || attribute_changed?
end
end
end
If you need to check that some attribute was changed, you need to call attribute_changed? method on the record and pass this attribute like this
return unless record.new_record? || record.attribute_changed?(attribute)
Or may be use metaprogramming method like this
return unless record.new_record? || record.public_send("#{attribute}_changed?")
Some notes:
In Ruby we usually use PascalCase for class names and snake_case for hash keys
Validations are used for data validation, not for some normalization. It is usually used to add validation errors

Custom validator with argument

I have the following class:
class PatientPaymentSpreadsheetRow < ApplicationSpreadsheetRow
include ActiveModel::Validations
validate :date_format
def date_format
unless value('Transaction').split('/').last.length == 4
errors.add('Transaction', 'date format invalid')
end
end
end
This particular validation happens to act on value('Transaction'). I'd like my validator to be sufficiently generic that I can pass in any value (e.g. value('Date of birth')) and have it act on that value.
How can I accomplish this?
This is an old question, but I think this is more in the ballpark of what you were asking.
ActiveModel::EachValidator allows you to pass in options as a hash, which will be available in the class instance with options.
You can set up the validation in your model like
class SomeModel < ApplicationRecord
validates :transaction, date_format: { value: 'Transaction' }
end
And the custom validator would look like:
class DateFormatValidator < ActiveModel::EachValidator
def validate_each(model, attribute, value)
return if options[:value].split('/').last.length == 4
# ^^^^^^^ hash you pass is here
model.errors.add(attribute, 'date format invalid')
end
end
You can write custom 'each validator', like described in Guides:
class DateFormatValidator < ActiveModel::EachValidator
def validate_each(model, attribute, value)
return if value.split('/').last.length == 4
model.errors.add(attribute, 'date format invalid')
end
end
And use it like this:
class MyCustomModel
include ActiveModel::Validations
attr_accessor :my_date_attr
validates :my_date_attr, date_format: true
end
But presumably you want the validations to run unconditionally, so...
validate :date_format
DATES_TO_VALIDATE = ['Transaction', 'Date of birth', 'Other date']
def date_format
DATES_TO_VALIDATE.each do |key|
unless value(key).split('/').last.length == 4
errors.add(key, 'date format invalid')
end
end
end
This can be extracted to an each_validator as per Marek Lipka's answer, with a custom constant DATES_TO_VALIDATE for each model and access it in the validator as model.class::DATES_TO_VALIDATE

Rails: validate by calling class method of other class

I have a lot of models that contains a field called source_name. I need to implement a validator in each of them that will check if the source_name lives up to curtain conditions.
Now I also have another class called SourceNameManager. In this model I have a method called valid_source_name? which takes a source_name_name and returns true or false.
What is the simplest way to make a validation that just validates source_name by calling the external service class SourceNameManager.valid_source_name?('some_name').
I was thinking about something like:
validates :source_name, ->(record) { SourceNameManager.valid_source_name?(record.source_name) }
but I don't think that works
Create a file app/models/source_name_validator.rb:
class SourceNameValidator < ActiveModel::EachValidator
validate_each(record, attribute, value)
unless SourceNameManager.valid_source_name?(value)
record.errors[attribute] << 'is not valid'
end
end
end
Then in each model where you want to validate the source name, add:
validates :source_name, source_name: true

rails serialized array validation

I need to validate email saving in email_list. For validation I have EmailValidator. But I cannot figure out how to use it in pair of validates_each. Are there any other ways to make validations?
class A < ActiveRecord::Base
serialize :email_list
validates_each :email_list do |r, a, v|
# what should it be here?
# EmailValidator.new(options).validate_each r, a, v
end
end
validates_each is for validating multiple attributes. In your case you have one attribute, and you need to validate it in a custom way.
Do it like this:
class A < ActiveRecord::Base
validate :all_emails_are_valid
...
private
def all_emails_are_valid
unless self.email_list.nil?
self.email_list.each do |email|
if # email is valid -- however you want to do that
errors.add(:email_list, "#{email} is not valid")
end
end
end
end
end
Note that you could also make a custom validator for this or put the validation in a proc on the validate call. See here.
Here's an example with a custom validator.
class A < ActiveRecord::Base
class ArrayOfEmailsValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
return if value.nil?
value.each do |email|
if # email is valid -- however you want to do that
record.errors.add(attribute, "#{email} is not valid")
end
end
end
end
validates :email_list, :array_of_emails => true
...
end
Of course you can put the ArrayOfEmailsValidator class in, i.e., lib/array_of_emails_validator.rb and load it where you need it. This way you can share the validator across models or even projects.
I ended up with this:
https://gist.github.com/amenzhinsky/c961f889a78f4557ae0b
You can write your own EmailValidator according to rails guide and use the ArrayValidator like:
validates :emails, array: { email: true }

How to make Rails' format with validator dynamic?

I am trying to validate the format of exchange_rate in my Invoice class:
class Invoice < ActiveRecord::Base
attr_accessible :currency, :exchange_rate
validates :exchange_rate, :format => { :with => exchange_rate_format }
private
def exchange_rate_format
if currency != user.preference.base_currency
DECIMAL_REGEX
else
ANOTHER_REGEX
end
end
end
The problem is: It doesn't work at all. I guess I need to use a Proc here? I never really figured out how to use it though. Maybe somebody can help.
Thanks a lot.
Yes, you need to use a Proc or lambda so that the validation will be called at runtime.
validates :exchange_rate, format: { with: ->(invoice) { invoice.exchange_rate_format } }
# Note, I used Ruby 1.9 hash and lambda syntax here.
To do this you'll need to move the exchange_rate_format out of the private methods list since we're defining an explicit receiver (invoice). You can make it protected instead, if you'd like. Or you can put the conditional into the lambda.
One way to do it is with a custom validator:
class Invoice < ActiveRecord::Base
class ExchangeRateFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if !value =~ record.exchange_rate_format
record.errors[attribute] << "your currency is weak sauce"
end
end
end
validates :exchange_rate, exchange_rate_format: true
# make public
def exchange_rate_format
if currency != user.preference.base_currency
DECIMAL_REGEX
else
ANOTHER_REGEX
end
end
end

Resources