Blog

Thoughts from my daily grind

Reform Rails - Validation - Wrong Number of Arguments Error

Posted by Ziyan Junaideen |Published: 02 April 2021 |Category: Ruby on Rails
Default Upload |

Lately Reform has been my go to method of implementing form objects in Ruby on Rails. I couple it with Dry Validation and I have a decent form mechanism for forms and validation in Rails web applications and deserialisation and validation of requests in APIs. Working on my LaundryRails project which is rocking Rails 6.1 on Ruby 3.

I experienced an error when trying to use the form class and it was rather a basic Reform class. A slimmed down implementation is as follows.

class OrderForm < Reform::Form
  property :name
  property :email
  property :phone

  # ...

  validation do
    params do
      required(:name).filled
    end
  end
end

For example using the OrderForm in the console gave an error like:

OrderForm.new(Order.new)
Traceback (most recent call last):
        3: from (irb):1:in `<main>'
        2: from app/forms/order_form.rb:1:in `<main>'
        1: from app/forms/order_form.rb:6:in `<class:OrderForm>'
ArgumentError (wrong number of arguments (given 1, expected 0))

Interestingly the error was pointing towards the validation do block. Which is unusual since I'd used it before. There were no Google hits either on the matter. This made me wonder if any other gem that I included might be running interference with Reform. After about an hour of eroding my hairline I found out why when I joined the Trailblazer Zulip Chat.

Reform support for Ruby 3.0 is still pending. The team has done most of the work and all but one issue has been resolved. The current pending broken test is related to the Representable GEM. We might have to downgrade to Ruby 2.7.2 for now until the update is available.

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments