Dynamic tag annotations

Hi there!

I was wondering if it’s possible to pass dynamic messages to the tag annotations.

example:

type user struct {
Name string valid:"required~Campo requerido"
}

type user struct {
Name string valid:"required~Required field"
}

I’m using govalidator package and for custumize your error messages you have to set them in the annotations (such as I show above), but my problem is that I’m building a multi-language website and I need to make dynamic the messages, so I can’t hardcode them.

You cannot change struct tags at runtime. If you know all the variants beforehand you might be able to declare structs with the required tag variants and convert between them. Otherwise you’ll need some validator that is more dynamic.

You mean that if I want to support different languages I should do the following:

// Spanish support
type user struct { Name stringvalid:“required~Campo requerido”}

// English support
type user struct { Name stringvalid:“required~Can’t be empty”}

and so on… woah thats a lot of repetition and work, maybe I should use a different package as you said :smile:

Just add a key to the tag, and use it to lookup the i18n message.

That’s sound great, could you please give me an example?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.