tcomb-form
tcomb-form copied to clipboard
Validation Scenario for required but not rendered fields: Uncaught TypeError: [tcomb] Invalid value undefined supplied to Struct
Version
Tell us which versions you are using:
- tcomb-form v0.9.10?
- tcomb v3.2.15
- react v15.4.2?
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
- Create a simple form with required fields
- Have valid values of respective types
- Use template to control whether to render a field or not, let's say don't render the field
- 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)

Thanks
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.