naymspace / backpex

Backpex is a highly customizable administration panel for Phoenix LiveView applications.
https://backpex.live
MIT License
360 stars 20 forks source link

Crash when saving form with readonly field #449

Open lukaszsamson opened 5 days ago

lukaszsamson commented 5 days ago

backpex version: 0.5.1

I have a simple form with one field that is editable only on new

login: %{
        module: Backpex.Fields.Text,
        label: "Login",
        readonly: fn assigns ->
          assigns.live_action != :new
        end
      },

It works correctly in new mode, in edit mode it loads and renders data but crashes on save

** (MatchError) no match of right hand side value: #Phoenix.LiveView.Socket<id: "phx-F99QwLg31MKc_iOE", endpoint: BackofficeWeb.Endpoint, view: BackofficeWeb.Live.BackofficeUserLive, parent_pid: nil, root_pid: #PID<0.1496.0>, router: BackofficeWeb.Router, assigns: %{show_form_errors: false, params: %{"backpex_id" => "6"}, resource_created_message: "Backoffice user has been created successfully.", save_label: "Save", action_type: nil, search_placeholder: "Search", init_order: %{direction: :asc, by: :id}, live_resource: BackofficeWeb.Live.BackofficeUserLive, live_action: :edit, create_button_label: "New Backoffice user", filters: [], metric_visibility: %{}, item: %BackofficeUser{__meta__: #Ecto.Schema.Metadata<:loaded, "backoffice_users">, id: 6, login: "cccc", }, singular_name: "Backoffice user", removed_uploads: [], panels: [], schema: BackofficeUser, total_pages: 1, changeset_function: #Function<4.56820374/3 in BackofficeWeb.Live.BackofficeUserLive.update_changeset>, fluid?: false, page_title: "Edit Backoffice user", filters_changed: false, full_text_search: nil, plural_name: "Backoffice users", active_fields: [login: %{active: true, label: "Login"}, active: %{active: true, label: "Active"}], action_to_confirm: nil, resource_actions: [], repo: Repo, select_all: false, __changed__: %{}, query_options: %{filters: %{}, order_by: :id, page: 1, per_page: 15, order_direction: :asc}, item_actions: [show: %{module: Backpex.ItemActions.Show, only: [:row]}, edit: %{module: Backpex.ItemActions.Edit, only: [:row, :show]}], fields: [login: %{label: "Login", module: Backpex.Fields.Text, readonly: #Function<10.56820374/1 in BackofficeWeb.Live.BackofficeUserLive.fields/0>}], items: [%BackofficeUser{__meta__: #Ecto.Schema.Metadata<:loaded, "backoffice_users">], return_to: "/admin/backoffice_users?order_by=id&page=1&per_page=15&order_direction=asc", id: :edit, searchable_fields: [], selected_items: [], flash: %{}, pubsub: %{name: Backoffice.PubSub, topic: "backoffice_users", event_prefix: "backoffice_user_"}, form: %Phoenix.HTML.Form{source: #Ecto.Changeset<action: nil, ...>, impl: Phoenix.HTML.FormData.Ecto.Changeset, ...}, item_count: 5, metrics: [], ...}, transport_pid: #PID<0.1478.0>, ...>
    (backpex 0.5.1) lib/backpex/live_components/form_component.ex:360: Backpex.FormComponent.handle_item_action/3
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/channel.ex:735: anonymous fn/4 in Phoenix.LiveView.Channel.inner_component_handle_event/4
    (telemetry 1.2.1) /Users/foo/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:209: Phoenix.LiveView.Diff.write_component/4
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/channel.ex:662: Phoenix.LiveView.Channel.component_handle/4
    (stdlib 5.2.3) gen_server.erl:1095: :gen_server.try_handle_info/3
    (stdlib 5.2.3) gen_server.erl:1183: :gen_server.handle_msg/6
    (stdlib 5.2.3) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{topic: "lv:phx-F99", event: "event", payload: %{"cid" => 11, "event" => "save", "type" => "form", "value" => ""}, ref: "31", join_ref: "27"}