2

Im learning Revel and using the Validation package to do some checks on input. I want to see if there already exists a record with a "name" in DB (i get a input from user through a form) and if true return error else create a record. Im am able to validate (with built in methods like Required, Maxlen ...) a field and display the error in HTML. But for my custom check Is adding a custom Validator to the Validation package the way to go or is there a way i can add custom keys and error to the Validation Context. I couldnt find how i can added custom keys and message to the error map. Thanks.

David Crowell
  • 3,711
  • 21
  • 28
broun
  • 2,483
  • 5
  • 40
  • 55
  • did you try reading the manual? http://revel.github.io/manual/validation.html Also, it may be useful to consider how the default validators are coded: https://github.com/revel/revel/blob/df5d88dbece276012b4c7ab45a33a1047c1db108/validators.go#L15-L47 Finally, are you trying to validate user input via an HTML form? From your description it doesn't seem clear how you're trying to use Revel's validation functionality. Maybe you're using it for the wrong purpose? – Brenden Oct 30 '14 at 00:01

1 Answers1

2

revel's validators.Validator interface looks like this:

type Validator interface {
    IsSatisfied(interface{}) bool
    DefaultMessage() string
}

And *validation.Validation has a method:

func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResult

And *validation.ValidationResult has a method:

func (*ValidationResult) Message

Putting that all together:

type usernameChecker struct {}

func(u usernameChecker) IsSatisified(i interface{}) bool {
    s, k := i.(string)

    if !k {
        return false
    }

    /* check if s exists in DB */
}

func(u usernameChecker) DefaultMessage() string {
    return "username already in use"
}

And in your application:

func (c MyApp) SaveUser(username string) revel.Result {
    c.Validation.Check(username, usernameChecker{}).Message("more specific or translated message in case of failure")
}

This is one if not the most badly designed validation library I have ever seen.

thwd
  • 23,956
  • 8
  • 74
  • 108
  • Ah, okay I did do some thing similar but tried invoking the validator through Validation directly. I will check this out, Thanks. Agree that this Validation library is convoluted, Im looking for Web frameworks for Golang and found Revel and Martini to be the major ones. Do you have any opinion on them or suggestions? – broun Jun 10 '14 at 06:31
  • Why are you looking for frameworks? Just use `net/http`; there's not much more any library or self-proclamed "framework" can offer you. – thwd Jun 10 '14 at 09:41
  • @tomwilde if you can improve the validation interface, then why not contribute it to Revel? I'm certain that even in its current form it will produce the desired outcome with fewer lines of code in comparison to a mish mash of `if` conditions and such. – Brenden Oct 29 '14 at 23:56
  • @Brenden why would I contribute to a project with which I don't agree? I'm not suggesting if-else as a solution. Just stating that the design is bad. – thwd Oct 30 '14 at 08:46
  • @Zahrec: I have yet to find one. Maybe I'll publish something of my own. We'll see. – thwd Jul 22 '15 at 06:55
  • @thwd you have a small typo. should be IsSatisfied not IsSatisified. I have to admit that it took me way too long to figure what I'm doing wrong – Eli DM May 28 '20 at 15:29