gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

optional refinement with custom error message not passing locals.error #230

Closed benmonro closed 8 years ago

benmonro commented 8 years ago

I have a case where my custom error message is not being triggered for a refinement which is wrapped by maybe. Consider the following schema & code:

var age = tcb.refinement(tcb.Number, n => n > 17, 'adult');
age.getValidationErrorMessage = function(value, path, context) {
  if(!age.is(value)) {
    return `${value} must be more than 17`;
  }
}
let Person = tcb.struct({
  name: tcb.Str,
  age: tcb.maybe(age),
  ageRequired: age
})

When I enter an invalid value for age (say 2 for example). The required version of age WILL get the error message passed to the template, but the optional one will not. I verified that the error is present when I call form.validate() but for some reason it's not being passed into the template. You can see the results here:

image

You'll notice that the optional field is _NOT_ displaying the error message (because locals.error is undefined for that field), but it _IS_ displayed for the required version of the same refinement.

benmonro commented 8 years ago

JSBin here: (thanks for that JSBin fix :smile: )

http://jsbin.com/vaponi/1/edit?js,console,output

gcanti commented 8 years ago

Thanks @benmonro, working on this...

gcanti commented 8 years ago

@benmonro I found a fix. Pushed to master and 0.6.x branches. Could you please give it a try before releasing?

benmonro commented 8 years ago

@gcanti you are a legend! works great, thanks!