Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error related value used in both formatted and raw ways #2

Open
lulalala opened this issue Apr 19, 2017 · 1 comment
Open

Error related value used in both formatted and raw ways #2

lulalala opened this issue Apr 19, 2017 · 1 comment

Comments

@lulalala
Copy link
Owner

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?

@javierjulio
Copy link

@lulalala I would think its better to still have the separate keys as the values are different and it helps to be explicit. Although thinking it through, if the error is for an attribute, the attribute would have the raw value to begin with meaning that its already provided to the error message translation as a %{value} param.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants