Overriding "can't be blank" message set by Ecto.Changeset.cast and validation messages in general

416 views
Skip to first unread message

Paulo Almeida

unread,
Mar 30, 2015, 11:20:20 AM3/30/15
to elixi...@googlegroups.com
Hello,

When using one of the built-in validation functions, like validate_format, it's possible to pass an optional :message to set the message on failure. But the validation message for required/mandatory fields is set by cast, which doesn't accept options. Is there currently a way to change the default "can't be blank" message, or do we need to implement cast/5 in Ecto.Changeset to accept an extra opts parameter?

Also related to this, if for example we want our application to support multiple languages, we can't set the message to be returned to the user at the model level (we don't know the user's language preference). So if the model holds the business rules and the user input validation rules, it should probably just return atoms that represent the kind of validation failure. Then only when rendering the view would this be mapped to an error message for the user.

Paulo

José Valim

unread,
Mar 30, 2015, 11:39:14 AM3/30/15
to elixi...@googlegroups.com
Right now, because we will be based on gettext in the future, I am thinking that, if you want to change this message, you just traverse the list afterwards, changing all "can't be blank" by something else. This is basically how gettext works anyway.

This would also answer your second question. gettext works on strings, so the translation happens on the strings.



José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/d5c0a081-eded-455e-94c2-b786e9603356%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages