Hello,
I’ve been looking for a solution to perform partial validation of a model. This is often an issue when you have multiple forms to fill a single model (ie with wicked wizard gem). They are multiple complicated solutions on the web but it would be fairly easy to implement inside the validation framework.
I can provide a patch if interested.
This could be the API:
my_object.save(partial_validation: array_of_attributes_to_validate)
The modified functions of active_record/validations (on branch 4.2)
def valid?(context = nil, partial_validation = nil)
context ||= (new_record? ? :create : :update)
output = super(context)
if partial_validation
(errors.keys - partial_validation).each do |attr|
errors.delete(attr)
end
end
errors.empty? && output
end
def perform_validations(options={}) # :nodoc:
options[:validate] == false || valid?(options[:context], options[:partial_validation].map(&:to_sym))
end
Let me know if interested
Regards, Thomas