Json / map input in a Phoenix form

I have a form in a Phoenix app, and I want one of the entries to accepts Json, which is then stored in the database as a map.
At the moment, I’m converting the Json input to a map just before running the Ecto.Changeset.cast function. The problem I’m having is that when editing or updating the form, or when handling errors, the converted changeset is then sent to the Phoenix form, and the form does not accept maps. I’ve tried converting the map back to Json at various stages, but I get either an error trying to update the changeset or a Phoenix form ‘wrong type’ error.
Does anyone know how to solve this problem? Alternatively, does anyone know of an example of using Json input in Phoenix forms?

6 Likes

Could you post your code @riverrun? I believe I’ve ran into a similar problem before and might be able to help.

2 Likes

Here’s a gist with the model, migration and form files. The controller is more or less the same as the one generated by mix phoenix.gen.html.
If you need any more info, let me know.

2 Likes

My permission low-level entries are raw json so I ended up building a custom form ‘element’ that renders it based on parent struct definitions. It is very situation specific though and it falls back to a text field if generic input is needed. Would be nice to have a built-in form element though, even if it did only fall back to a text field.

1 Like

That’s what I was planning on doing next :slight_smile:
I was just wondering if there was an easier way of handling this.