Skip to content

Instantly share code, notes, and snippets.

@cmrichards
Last active December 18, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmrichards/364920a5ffd6c49420c5 to your computer and use it in GitHub Desktop.
Save cmrichards/364920a5ffd6c49420c5 to your computer and use it in GitHub Desktop.
class BarcodeForm
include Virtus.model
include ActiveModel::Validations
attribute :barcode, String
attribute :job_number, Integer
attribute :sample_number, Integer
validates :other, :presence=>true
validate do
if barcode.present? && sample_details_present?
errors.add :base, "Can only enter data into barcode OR job/sample fields, not both"
elsif barcode.blank? && job_number.blank? && sample_number.blank?
errors.add :base, "You didn't enter anything!"
end
end
def sample_details_present?
job_number.present? || sample_number.present?
end
end
... controller ..
form = BarcodeForm.new params[:barcode]
if form.valid?
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment