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
?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With