Sha256: b1a6581bb30ed51f81622cde3cd2e973a6498c9283ab81049a233172f7ae7736
Contents?: true
Size: 525 Bytes
Versions: 3
Compression:
Stored size: 525 Bytes
Contents
class Reform::Form::UniqueValidator < ActiveModel::EachValidator def validate_each(form, attribute, value) # search for models with attribute equals to form field value query = form.model.class.where(attribute => value) # if model persisted, excluded own model from query query = query.merge(form.model.class.where("id <> ?", form.model.id)) if form.model.persisted? # if any models found, add error on attribute form.errors.add(attribute, "#{attribute} must be unique.") if query.any? end end
Version data entries
3 entries across 3 versions & 1 rubygems