Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
validation-messages-for-hanami
Advanced tools
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.
Add this line to your application's Gemfile:
gem 'validation-messages-for-hanami', '~> 0.1'
And then execute:
$ bundle install
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
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.
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
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
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
Bug reports and pull requests are welcome on GitHub at https://github.com/cabeza-de-termo/validation-messages-for-hanami.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that validation-messages-for-hanami demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.