-
-
Save cmrichards/364920a5ffd6c49420c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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