This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-18
Channels
- # aws (21)
- # babashka (32)
- # babashka-sci-dev (3)
- # beginners (17)
- # biff (1)
- # calva (8)
- # clj-kondo (1)
- # cljfx (8)
- # cljs-dev (3)
- # clojure (13)
- # clojure-belgium (1)
- # clojure-europe (16)
- # clojure-losangeles (2)
- # clojure-norway (6)
- # clojurescript (11)
- # conjure (1)
- # data-science (1)
- # fulcro (2)
- # gratitude (5)
- # helix (1)
- # joyride (3)
- # malli (14)
- # nbb (4)
- # off-topic (11)
- # other-languages (10)
- # polylith (4)
- # re-frame (2)
- # sci (3)
- # shadow-cljs (20)
- # spacemacs (3)
- # tools-deps (1)
- # vim (4)
Hi, I want to use malli for form-input validation... I want to display customs errors
Example: If the user did not type anything at all, I want to display "This field is required", is the user typed just one character, I want to display the following error msg "It should be at least 2 character long"
(def form-name [:string {:min 2
:fn {:error/fn '(fn [{:keys [value]} _]
(if (empty value)
"This field is required"
"It must be at least 2 character long"))}}])
(def form [:map [:name form-name]])
(me/humanize (m/explain form {:name ""}))
; The behaviour I want
; => "This field is required"
(me/humanize (m/explain form {:name "J"}))
; The behaviour I want
; => "It must be at least 2 character long"
I think your error is in your precondition, the function you should use is empty?
instead of empty
There is also something wrong, the fn function never evaluate. @UK0810AQ2 I want to custom the default error msg.
The property should be :error/fn
directly, without the preceding :fn
key
But what's wrong with the default errors?
:string {:error/fn {:en (fn [{:keys [schema value]} _]
(let [{:keys [min max]} (m/properties schema)]
(cond
(not (string? value)) "should be a string"
(and min (= min max)) (str "should be " min " characters")
(and min max) (str "should be between " min " and " max " characters")
min (str "should be at least " min " characters")
max (str "should be at most " max " characters"))))}}
And if it's in a map you'll get "missing required field blah" from there
Also, I think you should use empty?
and not empty
@UK0810AQ2 did you run this code on the repl? I coud not make it works.
Also, I copy and paste the following code from github, and I'm getting the following exception... What am I missing?
@U0103KEKSLR you need to add the sci dependency to your project if your functions are quoted