🚧 Initial work on password form.

This commit is contained in:
Andrey Antukh 2019-08-28 19:41:11 +02:00
parent 08bd135d55
commit ae2d8330ca
4 changed files with 129 additions and 105 deletions

View file

@ -44,9 +44,29 @@
([self f x y] (update-fn #(f % x y)))
([self f x y more] (update-fn #(apply f % x y more))))))
(defn- simplify-errors
(defn- translate-error-type
[type]
(case type
::stt/string "errors.should-be-string"
::stt/number "errors.should-be-number"
::stt/number-str "errors.should-be-number"
::stt/integer "errors.should-be-integer"
::stt/integer-str "errors.should-be-integer"
::stt/required "errors.required"
::stt/email "errors.should-be-valid-email"
::stt/uuid "errors.should-be-uuid"
::stt/uuid-str "errors.should-be-valid-uuid"
"errors.undefined-error"))
(defn- translate-errors
[errors]
(reduce-kv #(assoc %1 %2 (:message %3)) {} errors))
(reduce-kv (fn [acc key val]
(if (string? (:message val))
(assoc acc key val)
(->> (translate-error-type (:type val))
(assoc val :message)
(assoc acc key))))
{} errors))
(defn use-form
[{:keys [initial spec] :as opts}]
@ -54,7 +74,7 @@
:errors {}
:touched {}})
[errors' clean-data] (validate spec (:data state))
errors (merge (reduce-kv #(assoc %1 %2 (:message %3)) {} errors')
errors (merge (translate-errors errors')
(:errors state))]
(-> (assoc state
:errors errors
@ -86,11 +106,16 @@
(mf/defc error-input
[{:keys [form field] :as props}]
(let [touched? (get-in form [:touched field])
error? (get-in form [:errors field])]
(when (and touched? error?)
error (get-in form [:errors field])]
(when (and touched? error)
[:ul.form-errors
[:li {:key error?} (tr error?)]])))
[:li {:key (:type error)} (tr (:message error))]])))
(defn error-class
[form field]
(when (and (get-in form [:errors field])
(get-in form [:touched field]))
"invalid"))
;; --- Additional Validators
@ -298,11 +323,6 @@
[:ul.form-errors
[:li {:key error} (tr error)]]))
(defn error-class
[errors field]
(when (get errors field)
"invalid"))
(defn clear-mixin
[store type]
{:will-unmount (fn [own]