Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 337
rmagnum2002 Avatar asked Oct 19 '22 23:10

rmagnum2002


2 Answers

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.

like image 195
Paulo Henrique Avatar answered Oct 23 '22 03:10

Paulo Henrique


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.

like image 31
rmagnum2002 Avatar answered Oct 23 '22 02:10

rmagnum2002