Skip to content

Making a reusable component to display all fields from a schema and incrementally update #6

Open
@Danwhy

Description

@Danwhy

In making the form component from dwyl/adoro#108 reusable, I came across a few issues:

What we need: A function that can be called with a schema, that will autogenerate a form.

To make this generic, and usable for different projects, we need to either be able to access the View of the calling module, or pass the individual functions from the view (or the result of those functions) to the template. For example, we need to be able to call user_path in the template, but this function is defined in the UserWeb view. So we would either need to be able to use UserWeb, :view, pass the user_path function to the template, or pass the result of user_path to the template.

I had trouble managing to use the view of the calling module:

  • To access the name of the calling module, the function would need to be a macro
  • Once implementing it as a macro, I could access the name of the module, but could not use it. I got the following error:
== Compilation error in file lib/migrate_web/controllers/user_controller.ex ==
** (ArgumentError) invalid arguments for use, expected a compile time atom or alias, got: web_name

I then decided to try to pass down the result of the user_path function as an assign to the template. This worked, I was able to access the path in the template.

I then had to make sure I included all of the Phoenix View code that would normally come from something like UserWeb, :view:

use Phoenix.View, root: "lib/templates"
use Phoenix.HTML
import Phoenix.HTML.Form
import ReusableWeb.ErrorHelpers

But I then encountered the following error:

** (UndefinedFunctionError) function Phoenix.HTML.form_for/3 is undefined or private
        (phoenix_html) Phoenix.HTML.form_for(#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #Migrate.Accounts.User<>, valid?: true>, "/users/1", #Function<0.64785437/1 in Reusable.ReusableView."form.html"/1>)

It looks like I may have not included the Phoenix Ecto functions that implement the necessary protocol for the changeset to be used in form_for: phoenixframework/phoenix_html#85, so I'll look at that next

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions