gcanti / tcomb-form

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

Validation Scenario for required but not rendered fields: Uncaught TypeError: [tcomb] Invalid value undefined supplied to Struct #372

Open rajeshps opened 7 years ago

rajeshps commented 7 years ago

Version

Tell us which versions you are using:

Expected behaviour

Tell us what should happen

When any given field is marked mandatory/required but not rendered and if valid VALUE exist in context then there shouldn't be a validation error.

Actual behaviour

Tell us what happens instead

const ftype = t.struct({
    YesNo: t.Boolean,
    FirstName: t.String,
    LastName: t.String
})
var value = {
YesNo: false,
FirstName: "Jhon",
LastName: "Smith"
}

In the above simple Form of 3 fields, all are required fields. However, via templates when one of the fields are not rendered for given condition (let's say if true don't render LastName) and form only has the Checkbox and FirstName. When refs.form.validate() is triggered I get below error despite my value object having LastName value.

Uncaught TypeError: [tcomb] Invalid value undefined supplied to Struct{YesNo: String, FirstName: String, LastName: String}/LastName: String

No Error when LastName is made optional by t.maybe()

const ftype = t.struct({
    YesNo: t.Boolean,
    FirstName: t.String,
    LastName: t.maybe(t.String)
})

Also, No Error when LastName is displayed in form and obviously the value is populated to the field and validation passes through.

Steps to reproduce

  1. Create a simple form with required fields
  2. Have valid values of respective types
  3. Use template to control whether to render a field or not, let's say don't render the field
  4. On some action (button click) call this.refs.form.validate() -> Uncaught TypeError

Stack trace and console log

Uncaught TypeError: [tcomb] Invalid value undefined supplied to Struct{YesNo: String, FirstName: String, LastName: String}/LastName: String
    at Function.fail (fail.js:2)
    at assert (assert.js:14)
    at Irreducible (irreducible.js:18)
    at create (create.js:9)
    at new Struct (struct.js:82)
    at Struct.validate (components.js:661)
    at Form.validate (components.js:984)
    at ValidationForm._this.onConfirm (ValidationForm.jsx:50)
    at Button._this.handleClick (button.js:100)
    at Object.ReactErrorUtils.invokeGuardedCallback (ReactErrorUtils.js:70)

validation_error_1 validation_error_2 validation_error_4 validation_error_3

Thanks

compojoom commented 5 years ago

Hi, old issue, and most probably the problem is no longer relevant for you 2 years later, but for others that like me pull their hair at this...

I found this topic over here: https://github.com/gcanti/tcomb/issues/168

And it turns out that one can pass a default props to a struct. If you provide your values for the struct props there, then if the fields are missing in the template tcomb won't complain.

@gcanti - I think this can be closed.