formex icon indicating copy to clipboard operation
formex copied to clipboard

ecto changeset from schema

Open th31nitiate opened this issue 5 years ago • 8 comments

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

th31nitiate avatar Dec 28 '18 20:12 th31nitiate

You can pass your functions to the modify_changeset callback https://github.com/jakub-zawislak/formex_ecto#changeset-modification Is this what you need?

jakub-zawislak avatar Dec 28 '18 22:12 jakub-zawislak

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.

th31nitiate avatar Dec 29 '18 22:12 th31nitiate

Are you using ecto 3.0? I haven't noticed that 3.0 is already published 😄

jakub-zawislak avatar Dec 29 '18 22:12 jakub-zawislak

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

th31nitiate avatar Dec 29 '18 23:12 th31nitiate

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.

jakub-zawislak avatar Dec 29 '18 23:12 jakub-zawislak

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 ?

th31nitiate avatar Dec 30 '18 00:12 th31nitiate

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

Formex.Form struct docs

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:

jakub-zawislak avatar Dec 30 '18 10:12 jakub-zawislak

You can now install formex_ecto with ecto 3.0

jakub-zawislak avatar Dec 30 '18 21:12 jakub-zawislak