Sha256: 25e8f6bc8ce0e3aacf56bf2350932d649ec42230c87ffd5ad0d00e6765055cdd

Contents?: true

Size: 890 Bytes

Versions: 4

Compression:

Stored size: 890 Bytes

Contents

# Reform's own implementation for uniqueness which does not write to model.
class Reform::Form::UniqueValidator < ActiveModel::EachValidator
  def validate_each(form, attribute, value)
    model = form.model_for_property(attribute)

    # search for models with attribute equals to form field value
    query = model.class.where(attribute => value)

    # if model persisted, excluded own model from query
    query = query.merge(model.class.where("id <> ?", model.id)) if model.persisted?

    # if any models found, add error on attribute
    form.errors.add(attribute, "#{attribute} must be unique.") if query.any?
  end
end

# FIXME: ActiveModel loads validators via const_get(#{name}Validator). This magic forces us to
# make the new :unique validator available here.
Reform::Form::ActiveModel::Validations::Validator.class_eval do
  UniqueValidator = Reform::Form::UniqueValidator
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
reform-2.0.5 lib/reform/form/validation/unique_validator.rb
reform-2.0.4 lib/reform/form/validation/unique_validator.rb
reform-2.0.3 lib/reform/form/validation/unique_validator.rb
reform-2.0.2 lib/reform/form/validation/unique_validator.rb