#Formal: custom error msg

1 messages · Page 1 of 1 (latest)

polar fulcrum
#

Hey everyone,

I am playing around with the formal library and I was wondering what was the recommanded way to add an error message to a specifi action.

For example how could I add "password must be at least 3 characters" ?

I tried to look at the language function but that doesn't seem very ergonomic ?

fn signup_form() -> Form(Signup) {
  form.new({
    use email <- form.field("email", {
      form.parse_email
    })
    use password <- form.field("password", {
      form.parse_string
      |> form.check_string_length_more_than(3) // how can I add a specific error
                                               // message if this guard is failing ?
    })
    use _ <- form.field("confirm", {
      form.parse_string
      |> form.check_confirms(password)
    })
    SignUp(email: email, password: password)
  })
}
rustic moon
#

Formal errors don't have the name of the field in them

#

So you'd add the name of the field in your HTML if you want it

#

Otherwise that's the error already with the default translator for English

#

If you want to specify different a different translation for the built in errors then you want to use language to specify a custom translator function

#

If you want to add a new error that is not one of the built in ones then you can use check to add your own validation logic and custom error

#

But that's not just a new translation, as I believe you want from your question

polar fulcrum
#

Thanks Louis for the help, that cleared it 🙌

rustic moon
#

np!

#

The definition of the en_us translator may be a useful reference

pub fn en_us(error: FieldError) -> String {
  case error {
    MustBeColour -> "must be a hex color code"
    _ -> en_gb(error)
  }
}