Setting server messages doesn't work as intended.
conjurernix opened this issue · comments
I want to use fork.re-frame/set-server-message
to potentially set a server error from my fetch's on-fail handler to the form. From what i see in the fork.core
the message is set in the path [db path :server-message]
.
(defn set-server-message
[db path message]
(assoc-in db (concat (vectorize-path path) [:server-message]) message))
but this prop is not returned from fork.
(let [db @(rf/subscribe [::db path])
validation (when-let [val-fn (:validation props)]
(core/handle-validation @state val-fn))
server-validation (core/resolve-server-validation (:server db))
on-submit-server-message (:server-message db)]
[component
{:props (:props props)
:state state
:db db
:path path
:form-id form-id
:values (:values @state)
:dirty (core/dirty (:values @state) (merge (:initial-values @state)
(:touched-values @state)))
:errors validation
:server-errors server-validation
:on-submit-server-message on-submit-server-message
:touched (:touched handlers)
:set-touched (:set-touched handlers)
:set-untouched (:set-untouched handlers)
:submitting? (:submitting? db)
:attempted-submissions (or (:attempted-submissions @state) 0)
:successful-submissions (or (:successful-submissions @state) 0)
:set-values (:set-values handlers)
:disable (:disable handlers)
:enable (:enable handlers)
:disabled? (:disabled? handlers)
:normalize-name (:normalize-name handlers)
:set-handle-change (:set-handle-change handlers)
:set-handle-blur (:set-handle-blur handlers)
:handle-change (:handle-change handlers)
:handle-blur (:handle-blur handlers)
:send-server-request (:send-server-request handlers)
:reset (:reset handlers)
:handle-submit #(core/handle-submit % (merge props
{:state state
:path path
:server (:server db)
:form-id form-id
:validation validation
:already-submitting? (:submitting? db)
:reset (:reset handlers)}))}])
From what i understand the :on-submit-server-message
is supposed to reflect that, but it's being read from a different path [db :server-message]
which I guess is not correct as it would cause errors when rendering multiple forms (that's why you'd need the path).
Also using :server-message
instead of :on-submit-server-message
looks cleaner and more concise, also more consistent with the "setter".
My bad, I didn't notice this line of code:
(let [db @(rf/subscribe [::db path])
...
but I'd still prefer them to have a consistent naming.
I think I initially considered just :server-message
, but ended up with something more specific. Maybe :server-message
would be better, as I'm using that key in the re-frame db anyways. I think I'll update it thanks.