简体   繁体   中英

trailblazer reform gem, how to handle this type of input validation?

We are looking at use the reform gem for validating input.

One of the issues we are facing is that we accept input in this format:

params = {
  records: {
    "record-id-23423424": {
      name:       'Joe Smith'
    }
    "record-id-43234233": {
      name:       'Jane Doe'
    }
    "record-id-345234555": {
      name:       'Fox trot'
    }
    "record-id-34234234": {
      name:       'Alex'
    }
  }
}

so if we were to create reform class

class RecordForm < Reform::Form
  property :records
  validates :records, presence: true

  # ?????????
end

How do we validate the contents of the records to make sure each one has a name? The record-id-values are not known ahead of time.

Reform currently doesn't allow dynamic properties, and actually, it's not planned since Reform is supposed to be a UI-specific form object.

The solution would be to pre-parse your input into something what Laura suggests . You could then have nested properties for each field.

collection :records do
  property :id # manually parsed
  property :name
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM