maqnius
Hey there ![]()
I was trying to adapt the Contexts Article using the Form Struct and to_form.
I ended up having problems to render the input
<.input
field={@form[:categories]}
type="select"
multiple={true}
options={category_options(@article)}
/>
It errors like this
** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for type Ecto.Changeset (a struct).
because the value of the field @form[:categories] contains a Category changeset, introduced by the change_article\2 function - which is taken from the Context Article
def change_article(%Article{} = article, attrs \\ %{}) do
categories = list_categories_by_id(attrs["categories"])
article
|> Repo.preload(:categories)
|> Article.changeset(attrs)
|> Ecto.Changeset.put_assoc(:categories, categories)
end
Phoenix tries to render the value in the select component but the changeset doesn’t help much here
def input(%{type: "select"} = assigns) do
~H"""
<fieldset class="fieldset mb-2">
<label>
<span :if={@label} class="fieldset-label mb-1">{@label}</span>
<select
id={@id}
name={@name}
class={["w-full select", @errors != [] && "select-error"]}
multiple={@multiple}
{@rest}
>
<option :if={@prompt} value="">{@prompt}</option>
{Phoenix.HTML.Form.options_for_select(@options, @value)}
</select>
</label>
<.error :for={msg <- @errors}>{msg}</.error>
</fieldset>
"""
end
I guess, I understand the problem, but I’m unsure how to proceed from here. I wasn’t very successfull finding examples using to_form.
What should I change, the input template, the form template, the change_article function or something else?
Thank you for help <3
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jdiago
I think the select is supposed to be for
@form[:category_ids].The
many_to_many :categoriesin the schema takes thecategorieskey for itself so you can’t use it.maqnius
So would it be best to change the
change_articlefunction to populatecategory_ids?That would make it harder to be re-used in the update and create functions, as I would have to parse the IDs in both of them
jdiago
attrs["categories"]there needs to becategory_ids, too.list_categories_by_idandput_assocare doing the work of wiring up the associated data. Read more about it here: Ecto.Changeset — Ecto v3.14.0maqnius
Mh yes, but the changeset returned by
change_articleends up to build the form usingto_formatm:So
category_idshas to be in the changeset (if I’m not missing anything).But it also might just not work out this way, when m2m relations are involved.
Where would you inject
category_idsinto fhe form?jdiago
When you submit the form, part of the article params will be
category_ids. When that goes into the create or update context functions, those will callchange_articlewhere it gets passed tolist_categories_by_idand the result of that query gets shoved into the changeset byput_assoc.maqnius
Ahhh! Now I got it!
@form[:category_ids]in this case merely defines the name of the submitted value not the value itself.The actual submitted value is defined by the
options={category_options(@article)}which renders to options.I was wondering where
:category_idswould be populated, as only the changeset with:categoriesis assigned (as a HTML.Form):Thank you very much for clearing things up!
jdiago
I think the confusion is coming from the relationship between the
Phoenix.HTML.Formstruct that comes from theto_formfunction and what you pass to it(in this case%Article{})?In here Phoenix.HTML.Form — Phoenix.HTML v4.1.1 there’s this snippet:
maqnius
Yes
I noticed that it’s possible, but I didn’t understand when and how to make use of it.
The frameworks I used so far, expected to have a form data structure completely defined to render it properly and I treated the changeset as such in my mind.
jdiago
In Phoenix, if you want to sketch out a form quickly, you can use
to_form(%{}, as: "some_name"). You can then put whatever inputs to shape the params however you like.LostKobrakai
I’d suggest reframing “article form setting categories” to “article form managing article_categories”, so moving from many to many to has many relationship. Then you can use
inputs_for+selectforcategory_id.