Hi people, I was playing a bit with Phoenix components and I just realised that the way I wanted to use the components is a bit tricky, I mean, I was reading the autogenerated code for the core_components.ex
module, and I found that it’s using the internals of Phoenix.HTML
but not the public functions, so I was trying:
def simple_form(assigns) do
~H"""
<.form :let={f} for={@for} class="form" {@rest}>
<% assigns = assign(assigns, form: f) %>
<%= render_slot(@inner_block) %>
<hr/>
<p class="help">(*) <%= gettext("required") %></p>
<hr/>
<div class="field is-grouped">
<p class="control">
<%= Phoenix.HTML.Form.submit @submit, class: "button is-primary" %>
</p>
<p class="control">
<%= Phoenix.HTML.Link.link @cancel, to: @return_to, class: "button" %>
</p>
</div>
</.form>
"""
end
The main idea of the code is to provide for the elements inside of the render_slot
the @form
variable, but it’s not available and of course, the system is complaining because if I write:
<.simple_form for={@changeset} action={@action} return_to={@return_to}>
<.checkbox name={:enabled} label={gettext("Enabled?")} help={gettext("If enabled, it will appear in the public listing of books.")}/>
</.simple_form>
The implementation is:
def checkbox(assigns) do
~H"""
<div class="field">
<label class="label"><%= @label %></label>
<div class="control">
<label class={"switch is-rounded#{if(@form.errors[@name], do: " is-danger")}"}>
<%= Phoenix.HTML.Form.checkbox @form, @name %>
<span class="check"></span>
<span class="control-label"></span>
</label>
</div>
</div>
"""
end
so, well, the @form
isn’t available, is there a way to plug that from the simple_form
render function or should I change my approach?