一個簡單的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" %>