tfwright / live_admin

Low-config admin UI for Phoenix apps, built on LiveView
MIT License
248 stars 22 forks source link

Add virtual fields #87

Closed uwej711 closed 6 months ago

uwej711 commented 6 months ago

I just started to use live admin and wanted to use it to be able to create users (from mix phx.gen.auth) through the admin. The generated users have a virtual field that was not part of the admin interface.

tfwright commented 6 months ago

Hi @uwej711

Can you explain a bit more about your use case?

My reason for not currently including virtual fields here is that very commonly virtual fields are used for returning calculated data in Ecto queries, which would not be useful to include in the LiveAdmin UI, at least not by default.

uwej711 commented 6 months ago

Hi @tfwright,

OK, yes I understand. I will try to explain what I want to be able to do.

I have the following schema for a user:

schema "users" do
    field :username, :string
    field :password, :string, virtual: true, redact: true
    field :hashed_password, :string, redact: true

    timestamps(type: :utc_datetime)
end

The password field contains the plain text password. When it is set the validation will create a password hash and store it in hashed_password which is the field that is persisted.

So to enable an administrator to set the password for a user, I need the password field in the form (and maybe an additional password_confirmation field which is not in the schema at all).

So my first attempt was to look where the fields for the form come from and I saw that only "real" fields are used.

I think the feature I need is to add additional fields to the form (whether part of the schema or not). Those should also have type, so a simple list wont do.

That would not break BC for other users and be independent on virtual fields. If you are ok, I will give this new idea a try.

Kind regards Uwe

uwej711 commented 6 months ago

OK, adding random fields will not work easily (would need extra handling in build_changeset). I think I'm closing this right now and will just skip the feature I was trying to implement in my application.

tfwright commented 6 months ago

This is one of the main use cases of component overrides. The demo app includes a example of replacing the entire form behavior on a resource: https://github.com/tfwright/live_admin/blob/ca5e5dae2ddb06018b7800285b8f713320d75ccc/dev.exs#L323-L389

I would really like to come up with a cleverer way to incorporate virtual fields though so if you come up with other suggestions feel free to open a discussion thread.