Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show error messages under the field in Phoenix Framework

How can I show form errors under the fields instead of the top of the form?

How I can make something like this:

<%= text_input u, :username %>

to render something like this, if there is an error in this field ->

<div class="field-with-error">
  <input type="text">
  <span class="error">This username is already taken</span>
</div>
like image 223
NoDisplayName Avatar asked Aug 15 '15 11:08

NoDisplayName


2 Answers

Now you can use:

<%= error_tag f, :firstname %>
like image 171
Jeremie Ges Avatar answered Oct 28 '22 14:10

Jeremie Ges


The errors are all in the errors field of the form struct, so you can typically access them as f.errors. Here is an example:

<%= if message = f.errors[:username] do %>
  <span><%= translate_error(message) %></span>
<% end %>
like image 40
José Valim Avatar answered Oct 28 '22 14:10

José Valim