Displaying form validation messages next to input


#1

Hallo,

Could anyone share some advice on the topic subject?

I wonder what is the best way to display validation messages next to input they belong to.
So far I created a helper, which uses exposed array with those messages and checks if there are any messages to be displayed for this input. If there are, it generates some HTML with those messages and injects it in the place in template where it was called.

Template:

form_for :user, routes.login_path do text_field :login p display_errors :login text_field :password p display_errors :password submit 'Login' end

Helper:

module Panel module Helpers module FormError def display_errors(field) return nil if result.messages[field].nil? html.div do ul do result.messages[field].each do |m| li m.to_s end end end end end end end
Is this the proper way? Why do I need to prepend this helper with p to render the content of html returned by helper? Is it because it’s inside a form_for block? How do you deal with this?


#2

@refuse I already answered in chat, but replying again here for other peopler who want to ask the same question.

Until Hanami 0.7, you should build your own way to present errors. This will be improved with 0.8. Here’s how: https://github.com/hanami/validations#error-messages


#3

True, but still I don’t understand why I can’t render the value returned by helper.

I can replace the line (in template):
display_errors :login
with the code from helper:
div do ul do result.messages[:login].each do |m| li m.to_s end end
and it renders a list with messages, however it doesn’t work when I call my helper inside the template.
It only shows the messages if I prepend the call of the helper with p, but then the HTML is not valid.

Does anyone know how to solve this? Is it a bug or I’m doing it wrong?
Thanks