IHP Api Reference
Safe HaskellSafe-Inferred

IHP.ValidationSupport.Types

Synopsis

Documentation

attachValidatorResult :: (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> ValidatorResult -> model -> model Source #

attachFailure :: (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Text -> model -> model Source #

Adds a plain-text validation error to a record

Example:

>>> record |> attachFailure #email "should be a valid email"
User { .., meta = MetaBag { .., annotations = [ ("email", TextViolation "should be a valid email") ] } }

You can use this together with getValidationFailure

user
    |> attachFailure #email "cannot be empty"
    |> getValidationFailure #email

--  Returns: Just "cannot be empty"

If your error message uses HTML code, use attachFailureHtml.

attachFailureHtml :: (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Html -> model -> model Source #

Adds a validation error to a record. The error message can contain HTML code.

Example:

>>> record |> attachFailureHtml #email [hsx|should be a valid email. <a href="https://example.com/docs#email">Check out the documentation</a>|]
User { .., meta = MetaBag { .., annotations = [ ("email", HtmlViolation "should be a valid email. <a href="https://example.com/docs#email">Check out the documentation</a>") ] } }

You can use this together with getValidationViolation

user
    |> attachFailure #email "cannot be empty"
    |> getValidationViolation #email

--  Returns: Just (HtmlViolation "should be a valid email. <a href="https://example.com/docs#email">Check out the documentation</a>")

getValidationFailure :: (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Text Source #

Returns the validation failure for a field or Nothing

user
    |> attachFailure #email "cannot be empty"
    |> getValidationFailure #email

--  Returns: Just "cannot be empty"

When attachFailureHtml is used, this function will return HTML code:

user
    |> attachFailureHtml #url [hsx|Invalid value, check <a href="https://example.com">the documentation</a>|]
    |> getValidationFailure #url

--  Returns: Just "Invalid value, check <a href="https://example.com">the documentation</a>"

If you need to special-case validation errors with HTML code, use getValidationViolation

getValidationViolation :: (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Violation Source #

Similar to getValidationFailure, but exposes the information whether the error message contains HTML code

>>> user |> getValidationViolation #email
Just (TextViolation "cannot be empty")