一个简单的helper方法来解决这个问题 - >
def render_form_field(type, form, field, options \\ []) do
form_field = apply(Phoenix.HTML.Form, type, [form, field, options])
if form.errors[field] do
wrapper_class = "input field-with-errors"
error = content_tag(:span, form.errors[field], class: "error")
content_tag(:div, [form_field, error], class: wrapper_class)
else
wrapper_class = "input"
content_tag(:div, form_field, class: wrapper_class)
end
end
我已经很明显这里硬编码的几件事情,但那个会为例
做然后在模板中简单地执行以下操作 - >
<%= render_form_field :text_input, u, :username, placeholder: "blah blah" %>