ruby-on-railsrubyformsrubygemsreform

Populating single property in Reform


I'm using Reform gem. Let's say I have some form to which I pass two dates as two separate attributes in params. Now in form I want to instantiate another attribute (which is also existing model attribute) called interval to calculate interval between these two dates and then save it to model.

# some_form.rb

class SomeForm < Reform::Form
   property: :starts_on
   property: :ends_on
   property: :interval
end

What is the best way to populate interval based on incoming dates, validate it and save it on model?

I tried different things, from overriding validate method and using populator/prepopulator options, but to no avail.

In best scenario, I would assume it would look like this:

class SomeForm < Reform::Form
   property: :starts_on
   property: :ends_on
   property: :interval, populate: :calculate_interval
end

def calcute_interval
    (starts_on..ends_on).count
end

Solution

  • populator and prepopulator assume that the relevant properties are present in the incoming data. In your case, interval is not.

    Do you want to validate the interval value? If not, calculate interval in an Operation step and don't involve reform.

    If you want validation, your options are: