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