Sha256: dca20a3674e3626ef9166d53a4e0898906443274224eb9fe82b3d222f67d21a2
Contents?: true
Size: 701 Bytes
Versions: 138
Compression:
Stored size: 701 Bytes
Contents
# frozen_string_literal: true module Spree module Validations ## # Validates a field based on the maximum length of the underlying DB field, if there is one. class DbMaximumLengthValidator < ActiveModel::Validator def initialize(options) super @field = options[:field].to_s raise ArgumentError.new("a field must be specified to the validator") if @field.blank? end def validate(record) limit = record.class.columns_hash[@field].limit value = record[@field.to_sym] if value && limit && value.to_s.length > limit record.errors.add(@field.to_sym, :too_long, count: limit) end end end end end
Version data entries
138 entries across 138 versions & 2 rubygems