Phoenix liveview form without changeset10/31/2023 The article doesn’t say specifically because it focuses on slightly different things, I imagine the Context’s functions still return some Ecto data structures such as a Changeset on error and an Ecto Schema Struct on success. You can have more meaningful typespecs this way as well. So this takes what is a bit of a sticky layering where Ecto-friendly data structures flow in and out and separates it into something where you get well-structured input. And then you wall off using the database parts of Ecto using the Boundary library. The suggestion is to add a separate schemaless changeset in the Controller which knows how to validate and maybe even render the form. So do you abandon Ecto Changesets for casting and validation? How will you render forms conveniently? No-no. So Saša’s article suggests strictening the interface of your Context by using something like def create_product(name, price, description) do instead where you break apart the attributes and you can actually even guard the values explicitly. And maybe you don’t want those to be separated? Which is convenient when you need to change things but it doesn’t impose any real separation between Ecto and your Controller or LiveView. It lets us send in anything and sorts it out. get!( Product, id)Įnd def update_product(% Product would only fail if the changeset or database requires the name field to exist. all( Product)Įnd def get_product!(id), do: Repo. You can see the file here, I’ll strip out docs, aliases and imports to give a more skimmable thing and then we’ll try to say something about what it is.ĭefmodule Hello.Catalog do def list_products do Repo. The thing that is called a Context is in lib/hello/catalog.ex as the Hello.Catalog module. You can generate your own (this was 1.6.1) with mix Catalog Product products title:string or get the same as part of a mix call. If you check the Phoenix documentation you can see what’s generated by default. they will gladly help you generate a Context. The Phoenix generators are very helpful and if you are generating controllers etc. Then I’ll conclude with some kind of nuanced take that doesn’t really help you decide what you believe because that’s your job. So this will be an attempt to explain what Contexts as provided are, cover some common concerns around this and two rather opposing suggestions about how to deal with them. I don’t really care for controversy but what I see is a topic which gets confusing to wrangle with and which I never know quite how to explain. I’ve gathered that the discussions went high and low as it was becoming a thing in mainline Phoenix. The discussion of Contexts in Phoenix and their general usefulness feels like a common point of disagreement in Elixir.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |