I have a simple form with a textarea, where the user will type in (or copy'n'paste) a json string into it. The form will be sent to my controller action and I will validate the json string and if it will be ok, I will create some records and all is fine.
To send the form to my controller action I use Phoenix.HTML.Form "With connection data" at the moment and therefore I have no model/changeset.
<%= form_for @conn, @action, [as: :match], fn f -> %>
<%= textarea f, :json, rows: 20 %>
<%#= error_tag @changeset, f, :json %>
<% end %>
If the json is invalid for some reason, I like to render the form again and display the error message(s). error_tag
is a view helper method which will display an error at a field if there's a changeset. Because of this it's commented out now.
def error_tag(form, field) do
if error = form.errors[field] do
content_tag :span, (humanize(field) <> " " <> translate_error(error)), class: "help-block"
end
end
def error_tag(changeset, form, field) do
if changeset.action do
error_tag(form, field)
end
end
What is a proper way to add an error so that I can display them at a form field. Do I have to add the error to @conn
or f
(form) or Is there another way to got?