elm-form-validations icon indicating copy to clipboard operation
elm-form-validations copied to clipboard

Add support for custom validation messages

Open billperegoy opened this issue 8 years ago • 3 comments

Maybe I could use some sort of config object to override the default validation message.

billperegoy avatar Nov 29 '16 23:11 billperegoy

Maybe pass messages like a first optional parameter to a function?

passwordValidations : List Forms.FieldValidator
passwordValidations =
    [ Forms.validateExistence Just("Error message") ]

And in the validate function

validateExistence : Maybe ErrorMessage -> String -> Maybe String
validateExistence message string =
   let msg = 
       Maybe.withDefault "must be present" message
   in
   if String.length string > 0 then
        Nothing
    else
        Just msg

Or it looks too complicated?

alepop avatar Dec 05 '16 08:12 alepop

Thanks for the suggestion. That seems reasonable but I hate cluttering up the API for an optional argument. This is one area where I miss true optional params like you can have in JavaScript or Ruby. In Rails, you pass an optional hash with validation properties. So if there are no options, you pass nothing. In Elm, we have to pass a Nothing in so it clutters up the API in a way that doesn't thrill me.

I'm going to hold off acting on this until I get more feedback. I don't want to keep changing the API until I get some more input. I really appreciate you diving in and taking a look!

billperegoy avatar Dec 05 '16 14:12 billperegoy

Maybe adding a validateExistenceWithCustomMessage method? I understand the problem in change current API methods and cluttering them, but maybe adding a new method is not that worse. This method would then call the original validateExistence.

silaspedrosa avatar Oct 12 '17 14:10 silaspedrosa