diff --git a/lib/autoform.ex b/lib/autoform.ex index 760e57a..9014d62 100644 --- a/lib/autoform.ex +++ b/lib/autoform.ex @@ -41,6 +41,7 @@ defmodule Autoform do * `:exclude` - A list of any fields in your schema you don't want to display on the form * `:update_field` - The field from your schema you want to use in your update path (/users/some-id), defaults to id * `:assoc_query` - An ecto query you want to use when loading your associations + * `:btn_txt` - A string that will be used in the form submission button """ @spec render_autoform( @@ -74,6 +75,7 @@ defmodule Autoform do assigns = assigns + |> submit_btn_txt(options) |> Enum.into(%{}) |> Map.put_new(:changeset, schema.changeset(struct(schema), %{})) |> Map.put(:action, path(conn, action, schema, options)) @@ -115,6 +117,7 @@ defmodule Autoform do * `:update_field` - The field from your schema you want to use in your update path (/users/some-id), defaults to id * `:assoc_query` - An ecto query you want to use when loading your associations * `:path` - The endpoint you want your form to post to. Defaults using the calling modules schema name plus :update_field. If you are rendering multiple schemas, you will almost certainly need to use this field + * `:btn_txt` - A string that will be used in the form submission button """ @spec custom_render_autoform( @@ -180,6 +183,7 @@ defmodule Autoform do Autoform.CustomView, "custom.html", Keyword.get(options, :assigns, []) + |> submit_btn_txt(options) |> Map.new() |> Map.put_new(:changeset, first_schema.changeset(struct(first_schema), %{})) |> (fn m -> @@ -276,6 +280,11 @@ defmodule Autoform do defp schema_name(schema) do schema |> to_string() |> String.downcase() |> String.split(".") |> List.last() end + + defp submit_btn_txt(assigns, options) do + btn_txt_str = Keyword.get(options, :btn_txt, "Save") + Keyword.put_new(assigns, :btn_txt, btn_txt_str) + end end end end diff --git a/lib/templates/autoform/form.html.eex b/lib/templates/autoform/form.html.eex index cd3f8a7..299ba55 100644 --- a/lib/templates/autoform/form.html.eex +++ b/lib/templates/autoform/form.html.eex @@ -15,10 +15,10 @@
<%= label f, assoc[:name], class: "control-label #{if assoc[:name] in @required do "required" end}" %> <%= for a <- assoc[:associations] do %> - Enum.find(fn l -> Map.get(l, :name) == Map.get(a, :display) || Map.get(l, :type) == Map.get(a, :display) end) do "checked" end%> > @@ -29,6 +29,6 @@ <% end %>
- <%= submit "Save", class: "btn btn-primary" %> + <%= submit @btn_txt, class: "btn btn-primary" %>
<% end %> diff --git a/lib/templates/custom/custom.html.eex b/lib/templates/custom/custom.html.eex index a31d11d..fe098d9 100644 --- a/lib/templates/custom/custom.html.eex +++ b/lib/templates/custom/custom.html.eex @@ -42,6 +42,6 @@ <% end %>
- <%= submit "Save", class: "btn btn-primary" %> + <%= submit @btn_txt, class: "btn btn-primary" %>
<% end %> diff --git a/test/autoform_view_test.exs b/test/autoform_view_test.exs index c415d1e..70da1a9 100644 --- a/test/autoform_view_test.exs +++ b/test/autoform_view_test.exs @@ -38,5 +38,11 @@ defmodule AutoformViewTest do refute h =~ "for=\"address_line_1\"" assert t =~ "for=\"address_line_1\"" end + + test "Submit button text says Create", %{conn: conn} do + response = conn |> get(address_path(conn, :new)) |> html_response(200) + + assert response =~ "Create" + end end end diff --git a/test/support/test_autoform/lib/test_autoform_web/templates/address/new.html.eex b/test/support/test_autoform/lib/test_autoform_web/templates/address/new.html.eex index 6ab8e18..cacc22b 100644 --- a/test/support/test_autoform/lib/test_autoform_web/templates/address/new.html.eex +++ b/test/support/test_autoform/lib/test_autoform_web/templates/address/new.html.eex @@ -1,3 +1,3 @@

Addresses

-<%= render_autoform @conn, :create, TestAutoform.Address, assigns: [changeset: @changeset], exclude: [:line_1] %> \ No newline at end of file +<%= render_autoform @conn, :create, TestAutoform.Address, assigns: [changeset: @changeset], exclude: [:line_1], btn_txt: "Create" %>