formex
formex copied to clipboard
ecto changeset from schema
Is there a way to access change set that have been defined with the schema rather than defining the in the validator section of the form type. This will just greatly simplify the process of code organisation. This just means that we will have access to the change set from other parts of the project.
I am still reviewing the formex_ecto
You can pass your functions to the modify_changeset
callback
https://github.com/jakub-zawislak/formex_ecto#changeset-modification
Is this what you need?
Thanks for the response. This did not quite work since formex_ecto is not compatible with my version of ecto.
The best method is to change the flow. Use custom reg form to get simple, basic reg data i.e email, pass. Then use formex after email confirmation to gen other forms like updating the profile.
When it comes to schema validation we can use vex to add extra validation. This is because when we run the write the schema defined changeset is run against the input so validation always occurs.
I think this should work.
Are you using ecto 3.0? I haven't noticed that 3.0 is already published 😄
I managed to get the phoenix default
{:phoenix_ecto, "~> 4.0"},
{:ecto_sql, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
I am just checking now to see if the what I intend to do will work. I am looking forward to using the form template genarte and associations
I will upgrade the formex_ecto
to be compatible with ecto
3.0.
If the first question was about accessing ecto's changeset in pure formex
(without formex_ecto
), then it's impossible, because it doesn't use ecto at all.
Don't you think we could do something here on insert:
|> handle_form
|> case do
{:ok, article} ->
# do something with a new article struct
{:error, form} ->
# display errors
render(conn, "form.html", form: form)
end
We can call repo or something and submit what assume to be a map created from formex handle form ?
You can create a custom handle_form
function, that returns the whole form
struct:
def custom_handle_form(form) do
form = Formex.Builder.handle_submit(form)
if form.valid? do
{:ok, form} # I have changed `form.new_struct` to `form`
else
{:error, %{form | submitted?: true}}
end
end
then you could use it:
|> custom_handle_form
|> case do
{:ok, form} ->
# do something with a form
{:error, form} ->
# display errors
render(conn, "form.html", form: form)
end
You can use form.mapped_params
to create a changeset. But it's not that simple, as you can see in formex_ecto/changeset.ex. You can always create your own formex_ecto implementation if you don't like mine :smile:
You can now install formex_ecto
with ecto
3.0