lulalala / adequate_errors Goto Github PK
View Code? Open in Web Editor NEWOvercoming limitation of Rails model errors API
License: MIT License
Overcoming limitation of Rails model errors API
License: MIT License
When %{attribute}
or %{model}
are used in the error message, they are always capitalized. This can look weird when they are placed at the middle of the sentence.
This is minor though, as the easy workaround is to hard code attribute name in the translation.
Hi.
Since this gem intercepts Rails errors, does it mean that it is is Rails only? Or can it be used in other settings, like my own gems or Roda/Sinatra/Hanami?
When errors are added, its messages are evaluated immediately.
Ideally it should only be evaluated at the very last moment, e.g. when full_messages
are called.
user = User.new
I18n.with_locale(:en) { user.errors.add(:email, :invalid) }
I18n.with_locale(:pl) { user.error.full_messages } # => outputs PL errors
I18n.with_locale(:pt) { user.error.full_messages } # => outputs PT errors
I18n.with_locale(:pl) { user.error.full_messages } # => outputs EN errors always
Courtesy from @morgoth
I have a validation on an amount value. It is checked against a dynamic minimum value (think it as some currency exchange). So if the amount is not over the minimum value, the error message will say "You still need X more in order to reach the minimum".
The X here is the difference between user entered amount and the minimum. There is a requirement to have it formatted as currency, with dividers, e.g. $1,234.56.
Usually I can just say errors.add(:foo, :not_enough, lacking: format_currency(num))
to have the formatted text in the error messages.
However I also have a json api, which needs this value as float, for some branching logic. I can't just use the lacking
value in details, because it would be a string. I have to add the raw value errors.add(:foo, :not_enough, lacking: num, lacking_formatted: format_currency(num))
.
If I want to avoid adding two keys, one for raw value and one for formatted valu, what kind of API would help?
undefined method`foo' for #
triggered when calling form_object.errors.adequate.messages
.
Form objects usually need to copy error messages from inner objects to itself.
In the past when messages are eagerly evaluated, it works fine.
However in AdequateErrors, messages are lazily evaluated. So when we call form_object.errors.adequate.messages
, it will try to access the attribute from form object, which results in undefined method error.
Possible solution is to:
Sometime we want to not prefix the error message with attribute name. Assigning error to :base
works but what if attribute is important (for example to be included in json).
That is, to make attribute prefixing optional.
Sometimes it is desired to merge errors from associations or child models. For example form objects need to merge validation errors from its child models.
However, if we are to merge, it may not be possible to copy across the details
and message
at the same time. If we merely copied the details, those details in the form object probably won't be able to generate messages, because it does not have corresponding locale translations on form object scope.
That leaves us having to copy across the full messages only, losing details
.
An example of this is in ActiveInteraction https://github.com/AaronLasseigne/active_interaction/blob/v4.0.0/lib/active_interaction/errors.rb#L136-L143
This can be solved by introducing the idea of nested error object.
Sometimes it is desirable to access a specific error details
foo.errors.details[:amount].find { |error| error[:error] == :too_small }
It would be better to have a method to allow for this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.