rmagnum2002
rmagnum2002

Reputation: 11421

simple_form and custom validation message

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?

Upvotes: 5

Views: 1761

Answers (2)

Paulo Henrique
Paulo Henrique

Reputation: 1025

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.

Upvotes: 2

rmagnum2002
rmagnum2002

Reputation: 11421

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.

Upvotes: 1

Related Questions