Skip to content

Instantly share code, notes, and snippets.

@kemenaran
Created March 26, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemenaran/89beb48550b6e2845d57c16fc823fed6 to your computer and use it in GitHub Desktop.
Save kemenaran/89beb48550b6e2845d57c16fc823fed6 to your computer and use it in GitHub Desktop.
Format error messages for champs
validates_associated :champs, :champs_private
# -> Rails displays: "Champs is invalid"
validates_associated :champs, :champs_private, message: -> (object, _data) {
(object.champs + object.champs_private)
.reject(&:valid?)
.flat_map do |champ|
champ.errors.full_messages.map do |message|
"#{champ.libelle} : #{message}"
end
end
.to_sentence
}
# -> Rails displays: "Champs Carte d'identité : Le fichier n’est pas d’un type accepté"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment