Skip to content

Instantly share code, notes, and snippets.

@cmrichards cmrichards/gist:364920a5ffd6c49420c5 Secret
Last active Dec 18, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.