I want to build it in a way so that a user can enter multiple tags in an input separated by comma (tag1, tag2, tag3) but I always get invalid input on form validation.
So, my question is, how to properly get multiple tags and save them as an array so that Liveview form validation passes?
@LostKobrakai I followed the tutorial you’ve sent me and it worked perfectly, data is saved to the database, but how do I get it back? As there are multiple tags they come back inside a list.
Yeah, I get that, but I’m struggling with getting it back to LiveView for the last 2 hours and to be honest, I have no idea how to even approach this, everything I tried so far failed…
video = Videos.get_video_by_slug!(video_slug)
split_tags =
video
|> Map.get(:tags, [])
|> Enum.map(fn t -> t.name end) |> Enum.join(", ")
form =
video
|> Videos.change_video()
data = form.data |> Map.put(:split_tags, split_tags)
form = form |> Map.put(:data, data) |> to_form()
And since you followed the link then when you’re handling the save the split_tags will be sent in the params and you just overwrite the tags param with the split_tags and off you go.
The cleanest way would be using virtual fields and resolving it in the context layer.