Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule MyAppWeb.InputHelper do
use Phoenix.HTML
def input(form, field, opts \\ []) do
type = Phoenix.HTML.Form.input_type(form, field)
wrapper_classes = Keyword.get_values(opts, :wrapper_class) ++ ["row", "form-group"]
inputs_classes = Keyword.get_values(opts, :input_class) ++ ["form-control"]
if form.errors[field], do: inputs_classes = inputs_classes ++ ["form-control-dange"]
if form.errors[field], do: wrapper_classes = wrapper_classes ++ ["has-danger"]
wrapper_opts = [class: Enum.join(wrapper_classes, " ")]
input_opts =
[class: Enum.join(inputs_classes, " ")] ++
Keyword.drop(opts, [:wrapper_class, :input_class])
label = Keyword.get(opts, :label) || humanize(field)
if input_validations(form, field) |> Keyword.get(:required) do
label = label <> " *"
input_opts = input_opts ++ [required: "required"]
end
content_tag :div, wrapper_opts do
[
content_tag :span, class: "col-sm-2" do
label(form, field, label, class: "col-form-label float-sm-right")
end,
content_tag :div, class: "col-lg-6 col-md-10" do
input = apply(Phoenix.HTML.Form, type, [form, field, input_opts])
error = MyAppWeb.ErrorHelpers.error_tag(form, field)
[input, error || ""]
end
]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.