ruby-on-railstrailblazerreform

Getting "undefined method 'rule'" on adding a custom rule in reform form


I have just added reform gem and trying to create my first form. I'm writing a custom rule to validate 'email' property. this is my form

class StudentForm < Reform::Form
  feature Reform::Form::Dry

  property :first_name, validates: {presence: true}
  property :last_name, validates: {presence: true}
  property :email

  validation name: :email do
    rule(:email) do
        unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
            key.failure('has invalid format')
        end
    end
  end
end

I get this when I do StudentForm.new

undefined method `rule' for #<Reform::Form::ActiveModel::Validations::Group:0x00007f63d014c068 @validations=#<Class:0x00007f63d013ff70>> (NoMethodError)

I have also created an initializer which makes sure reform uses Dry validation instead of active model, as recommended in the docs

require "reform/form/dry"
Reform::Form.class_eval do
  feature Reform::Form::Dry
end

I think it is still using active model, but I'm not sure how to fix it. I did what the docs said.


Solution

  • The docs say you should be doing this to indicate you want to use dry validations:

    Rails.application.config.reform.validations = :dry