Validation messages for Hanami

A helper to customize the validation messages in Hanami application views.

Sometimes you need to customize a validation message based not only on the validation type and value, but also on the rendering context. That is, you may want the same validation on the same attribute with the same value to show different messages depending on the view you are rendering it. This helper allows you to do that nicely.

Actually this helper does not depend on Hanami::View at all (only on Hanami::Validations, and even that could be easily tweaked using a different DisplayableError adaptor), so it also could be used with other frameworks different than Hanami, but since Hanami is the framework that makes me feel good, the one that I love and choose, I don't give a shit about that.

Status

Gem Version Build Status Coverage Status

Installation

Add this line to your application's Gemfile:

gem 'validation-messages-for-hanami', '~> 0.1'

And then execute:

$ bundle install

Usage

Configuration

If you want to customize the messages only in a particular view, do

require 'validation-messages-for-hanami'

module Web::Views::Person
    class Create
        include Web::View
        include CabezaDeTermo::ValidationsMessages::Behaviour

        validation_messages do
            at :presence, on: 'person.address.street' do |error| 
                "Please, fill in some contact address street."
            end

            at :presence, on: 'person.address.country' do |error| 
                "Please, choose a country from the list."
            end
        end
    end
end

If you want to include the helper in all the views in the application, in your application.rb include

require 'validation-messages-for-hanami'

and then

# Configure the code that will yield each time Web::View is included
# This is useful for sharing common functionality
#
# See: http://www.rubydoc.info/gems/hanami-view#Configuration
view.prepare do
    include Hanami::Helpers
    include Web::Assets::Helpers

    include CabezaDeTermo::ValidationsMessages::Behaviour
    # If you want to customize the default messages for this application
    # uncomment the next line and set your own validation messages library
    # use_validation_messages_library WebValidationMessages.library

and now you can just use it in your views like

module Web::Views::Person
    class Create
        include Web::View

        validation_messages do
            at :presence, on: 'person.address.street' do |error| 
                "Please, fill in some contact address."
            end

            at :presence, on: 'person.address.country' do |error| 
                "Please, choose a country from the list."
            end
        end
    end
end

Customizing the validation messages

To customize a validation message based on the validation type do

module Web::Views::Person
    class Create
        include Web::View

        validation_messages do
            at :presence do |error| 
                "#{error.attribute_name} must be present"
            end
        end
    end
end

To customize a validation message for a particular attribute do

module Web::Views::Person
    class Create
        include Web::View

        validation_messages do
            at :presence, on: 'person.address.country' do |error| 
                "Please, choose a country from the list."
            end
        end
    end
end

To customize the displayable name of an attribute in all the validation messages do

module Web::Views::Person
    class Create
        include Web::View

        validation_messages do
            display :street, as: 'contact street address'

            at :presence do |error| 
                "#{error.attribute_name} must be present"
            end
        end
    end
end

and now error.attribute_name for :street attribute instead of displaying 'street' will display 'contact street address' in all the validation occurrences.

Customizing the default validations messages globally

If you want to change the default validation messages globally in all your applications do

CabezaDeTermo::ValidationMessages::Library.default.configure do
    message_for :presence do |error|
        "can not be left blank"
    end

    message_for :acceptance do |error|
        "must be accepted"
    end

    message_for :confirmation do |error|
        "doesn't match"
    end

    message_for :inclusion do |error|
        "isn't included"
    end

    message_for :exclusion do |error|
        "shouldn't belong to #{ Array(error.expected).join(', ') }"
    end

    message_for :format do |error|
        "doesn't match expected format"
    end

    message_for :size do |error|
        "doesn't match expected size"
    end
end

Customizing the default validation messages for a single application

If you want to customize the validation messages only for an application, in the application.rb file do

# Configure the code that will yield each time Web::View is included
# This is useful for sharing common functionality
#
# See: http://www.rubydoc.info/gems/hanami-view#Configuration
view.prepare do
    include Hanami::Helpers
    include Web::Assets::Helpers

    include CabezaDeTermo::ValidationsMessages::Behaviour
    use_validation_messages_library WebValidationMessages.library

and define

class WebValidationMessages
    def self.library
        CabezaDeTermo::Validations::Messages::Library.new do
            message_for :presence do |error|
                "can not be left blank"
            end

            message_for :acceptance do |error|
                "must be accepted"
            end

            message_for :confirmation do |error|
                "doesn't match"
            end

            message_for :inclusion do |error|
                "isn't included"
            end

            message_for :exclusion do |error|
                "shouldn't belong to #{ Array(error.expected).join(', ') }"
            end

            message_for :format do |error|
                "doesn't match expected format"
            end

            message_for :size do |error|
                "doesn't match expected size"
            end
        end
    end
end

Customizing the default validation messages for a single view

If you want to customize the validation messages only for a view, in the view do

module Web::Views::Person
    class Create
        include Web::View

        validation_messages do
            use_validation_messages_library SomeValidationMessages.library
        end
    end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cabeza-de-termo/validation-messages-for-hanami.

License

The gem is available as open source under the terms of the MIT License.