elm-form-validations
elm-form-validations copied to clipboard
Add support for custom validation messages
Maybe I could use some sort of config object to override the default validation message.
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?
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!
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
.