Setting messages_file

Hi. I just started using DryValidation (enjoying it so far:-), but I’m facing a strange issue and I couldn’t find any example and it seems like no one has encountered this before.

The problem is that I get NoMethodError: undefined method messages_file= exception whenever I try to set a messages_file via configure.

Am I missing something?

BTW, I’m using dry-validation-0.9.3 with reform-2.2.1.

Can you show the code snippet, where you try to set messages_file?

From integration tests, API is

      Dry::Validation.Schema do
        configure do
          config.messages_file = SPEC_ROOT.join('fixtures/locales/en.yml')
        end

        required(:email, &:filled?)
      end

Sure. It’s pretty simple though.

class UserAuthenticationForm < Reform::Form
  property  :email

  validation :default do
    configure { |config| config.messages_file = 'config/error_messages.yml' }
    
    required(:email).filled(:str?)
  end

end

And in reform.rb initializer, I have

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

whenever I try to initialize a new instance of class (via UserAuthenticationForm.new(User.new)), I get:
NoMethodError: undefined method `messages_file=' for #<Class:0x007fc2918316d8> Did you mean? messages

I finally managed to resolve the issue by specifying messages_file this way:

configure { |config| config.config.messages_file = 'config/error_messages.yml' }

Still don’t have any idea why this is happening :expressionless:

This feels like it might be a quirk of Reform’s integration with dry-validation? Might be worth reaching out to the Trailblazer people in gitter or elsewhere?

This should be:

configure { config.messages_file = 'config/error_messages.yml' }

Why did you think you need to pass config to the block?

1 Like

@Arvinje be aware that some of Reform’s documentation is still in the old (0.6) dry-v. You should look at the tests whilst we update the docs.

@timriley This isn’t a querk with Reform as a Reform validation block is just passed directly to a Dry::Validation.schema. @solnic has the answer, you no longer need the config variable in the configure block and it should work.

1 Like

Because the docs on the trailblazer site use the old code:
http://trailblazer.to/gems/reform/validation.html#dry-error-messages

1 Like