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

446 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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