0
votes

I'm having this form

<%= simple_form_for :post do |f| %>
  <%= f.error_notification %>
  <%= f.input :title %>
  <%= f.input :body %>
  <%= f.button :submit %>
<% end %>

The title field in my example is required. So if I try and submit the form without any value in the :title, the validation fails and comes back with an error message, as expected. However the error I get by default is just this can't be blank, so I was wondering if there's a way to display the full message of validation errors (like when not using simple_form gem), like The title field can't be blank etc

Is there a way t display the full messages?

3
on top or inline errors?Deepak Mahakale
@Sajan I believe that's not the issueDeepak Mahakale

3 Answers

1
votes

Not very clean, but it works :)

<%= simple_form_for :post do |f| %>
  <%= f.error_notification %>
  <%= f.input :title, error: f.object.errors.full_messages_for(:title).to_sentence %>
  <%= f.input :body, error: f.object.errors.full_messages_for(:body).to_sentence %>
  <%= f.button :submit %>
<% end %>
1
votes

Try using the full_error method, something like this:

<%= f.input :title, error: f.full_error(:title) %>
0
votes

Upon inspection, I found out that you can configure this globally:

config/initializers/simple_form.rb

SimpleForm.setup do |config|
  config.wrappers :default, class: :input,
    hint_class: :field_with_hint, error_class: :field_with_errors do |b|
    # ...
    # ...
    # COMMENT THIS LINE JUST LIKE THIS:
    # b.use :error, wrap_with: { tag: :span, class: :error }

    # THEN UNCOMMENT THIS LINE JUST LIKE THIS:
    b.use :full_error, wrap_with: { tag: :span, class: :error }
  end
end

Tested working.