Formalism R18n Errors
Errors for Formalism via R18n.
Installation
Add this line to your application's Gemfile:
gem 'formalism-r18n_errors'
And then execute:
bundle install
Or install it yourself as:
gem install formalism-r18n_errors
Usage
require 'formalism/r18n_errors'
module MyProject
module Forms
class Base < Formalism::Form
include Formalism::R18nErrors
def initialize(*)
## This is an example, but some value is required
@errors_key = self.class.name.split('::')[2].underscore.to_sym
super
end
end
class GeoLocation < Base
field :address
end
class User < Base
field :name, String
## `:errors_key` can be changed for nested forms
nested :location, GeoLocation, errors_key: :geolocation
private
def validate
## `add_error` is the alias for `errors.add errors_key, *` (`:user` in this case)
add_error :name, :is_empty if name.to_s.empty?
end
end
end
end
Validation Helpers
require 'formalism/r18n_errors/validation_helpers'
module MyProject
module Forms
class Base < Formalism::Form
include Formalism::R18nErrors
include Formalism::R18nErrors::ValidationHelpers
# ...
end
class User < Base
field :name, String
field :country
field :city
field :score, Float
field :email, String
nested :location, GeoLocation
private
def validate
## `errors.user.name.not_entered`
validate_entry :name
## `errors.user.country.not_chosen`
## `errors.user.city.not_chosen`
validate_choice %i[country city]
## `errors.user.location.not_provided`
validate_provision :location
## `errors.user.name.greater_than(20)`
validate_max_length :name, 20
## `errors.user.name.less_than(3)`
validate_min_length :name, 3
## `errors.user.score.greater_than(5)`
## or
## `errors.user.score.less_than(1)`
validate_range_entry :score, 1..5
## Requires `email_address` gem
## `errors.user.email.not_valid_email`
validate_email :email
## `errors.user.key.not_valid_uuid`
validate_uuid :key
## Requires `formalism-model_form` gem
## `errors.user.email.already_taken`
validate_uniqueness :email
## `errors.user.itself.already_exists.country_and_city`
validate_uniqueness %i[country city]
end
end
end
end
Development
After checking out the repo, run bundle install
to install dependencies.
Then, run toys rspec
to run the tests.
To install this gem onto your local machine, run toys gem install
.
To release a new version, run toys gem release %version%
.
See how it works here.
Contributing
Bug reports and pull requests are welcome on GitHub.
License
The gem is available as open source under the terms of the MIT License.