Fork me on GitHub

Hi, I want to use malli for form-input validation... I want to display customs errors


How could I display error msg base on the input value?


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"

George Peristerakis08:06:46

I think your error is in your precondition, the function you should use is empty? instead of empty

Ben Sless08:06:26

I think the defaults already give you this behavior. Try deleting the fn property


There is also something wrong, the fn function never evaluate. @UK0810AQ2 I want to custom the default error msg.

Ben Sless15:06:25

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)]
                                 (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?

Ben Sless03:06:14

I copied this example from malli's source, I assume it works


@U0103KEKSLR you need to add the sci dependency to your project if your functions are quoted


Thanks in advance