Replies: 5 comments 4 replies
-
|
This renders the inputs, without any form tag: f = form_for @changeset, "#", [id: @id]
h4 do: "Content"
hidden_input f, :id
label f, :name
text_input f, :nameThis renders a form tag with seemingly correct markup: form_for @changeset, "#", [id: @id], fn f ->
h4 do: "Content"
hidden_input f, :id
label f, :name
text_input f, :name
endThis causes an exception: form_for @changeset, "#", [id: @id], fn f ->
h4 do: "Content"
hidden_input f, :id
label f, :name
text_input f, :name
label f, :my_list
select f, :my_list, ["A", "B"]
endStacktrace: |
Beta Was this translation helpful? Give feedback.
-
|
With temple, it's currently not possible to emit a match into the markup, as you generally never want to do that, except for using Currently my workaround for this is to use a component, but this is still sort of hacky. The thing I'm currently working on are "slots" which would make this much better. Slots would enable you to pass data from inside a component back into the "children" or "inner_content". Anyways, this is what I do currently# component module for the live form
defmodule MyAppWeb.Component.LiveFormFor do
use MyAppWeb, :component
render do
@form
@inner_content
"</form>"
end
end
# usage of the live form
f =
form_for @changeset, "#",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save"
c LiveFormFor, form: f do
label f, :foo_id, "Foo"
select f, :foo_id, Foo.options(), prompt: "Select a foo..."
error_tag(f, :foo_id)
c Save, text: "Submit!" # this is another component, can be ignored for this example
endOnce I have slots completed, you would be able to write something similar to this# component module for the live form
defmodule MyAppWeb.Component.LiveFormFor do
use MyAppWeb, :component
render do
@form
slot :form, f: @form
"</form>"
end
end
# usage of the live form
c LiveFormFor, form: form_for @changeset, "#", phx_target: @myself, phx_change: "validate", phx_submit: "save" do
slot :form, %{f: f} do
label f, :foo_id, "Foo"
select f, :foo_id, Foo.options(), prompt: "Select a foo..."
error_tag(f, :foo_id)
c Save, text: "Submit!" # this is another component, can be ignored for this example
end
endRegarding your first exampleYour first example would work if you added a line with the form variable and a closing tag. The need for the `f` line is because temple emits `<% ... %>` for matches (`=`). This is because you normally wouldn't want to emit binding a variable into the markup. The need for `""` is because that is what [`form_for/3`](https://hexdocs.pm/phoenix_html/2.14.3/Phoenix.HTML.Form.html#form_for/3) needs.f = form_for @changeset, "#", [id: @id]
f
h4 do: "Content"
hidden_input f, :id
label f, :name
text_input f, :name
"</form>
Regarding your third exampleI believe that is because you didn't configure an alias for <select <%= Temple.Private.runtime_attrs(f) %> ...></select>instead of Here are the docs on that. I think I need to update the to include select. |
Beta Was this translation helpful? Give feedback.
-
|
I think that this might also stop being a problem sooner or later, as the 3 arity version of form_for has become deprecated: phoenixframework/phoenix_html#329 |
Beta Was this translation helpful? Give feedback.
-
|
I did attempt to set an alias I'll play around with the component when I have some time. It would be nice to do everything in temple, instead of a mix of leex. |
Beta Was this translation helpful? Give feedback.
-
|
FYI based on your component example + an alias, I just got this to work. Thank you for the quick feedback! Hopefully this discussion is useful to others. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The one thing I’ve been struggling with is (0.6.0-rc.0) forms with changesets in liveview. Not a pressing issue, since I’ve gone back to .leex for my affected pages, but it would be nice to convert those to temple if possible.
It seems like there's a problem with
selecttags, but I'm having trouble pinning down the issue.Another thing is that LiveView really prefers
form_for/3with assignment to a variable, whereas I could only get the form tag to renderform_for/4and a nested function.Examples to come in a sec.
Beta Was this translation helpful? Give feedback.
All reactions