5
votes

Intro: Using simple_form in app, added validations to attributes and errors display properly next to each field in form. Also I have 2 custom validation that adds custom error messages not related to attributes of the model.

The problem: In a basic rails form the errors are displayed above the form and also the ones from custom validation appears. But how can I show custom validation messages using simple_form?

2

2 Answers

2
votes

I would recommend you use a form object where you don't need to save errors in the base of the object. You would just create new attributes with those validations and add it to the view which would work out of the box with simple_form.

1
votes

I came up with this so far:

  <% if @object.errors.messages[:base].present? %>
    <ul class="error_messages_container">
    <% @object.errors.messages[:base].each do |e| %>
      <li><%= e %></li>
    <% end %>
    </ul>
  <% end %>

that I placed above the form, so I'll have my custom validation messages above the form. Also, waiting for other ideas.