Simple_form and custom validation message

Introduction: Using simple_form

in the application, the added attribute and error checks appear correctly next to each field in the form. Also, I have 2 custom validations that add custom error messages not related to model attributes.

Problem: In basic rails forms, errors are displayed above the form, and also those that cause custom validation appear. But how can I show custom validation messages using simple_form

?

+3


source to share


2 answers


I would recommend that you use a form object where you don't need to store errors in the base

object. You will simply create new attributes with these checks and add them to the view, which will work out of the box with simple_form

.



+1


source


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 %>

      



which I placed above the form, so I will have my custom validation messages above the form. Also, waiting for other ideas.

+1


source







All Articles