gcanti / tcomb-form

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

Default value #213

Closed benmonro closed 8 years ago

benmonro commented 8 years ago

Question. I have a form with some optional fields. When the data is submitted, if the optional fields are left blank they have a value of null. I'm wondering if there's a way to change the default to be empty string or something else...

gcanti commented 8 years ago

I'm wondering if there's a way to change the default to be empty string or something else

Have you tried with a custom transformer? Personally I try to keep my forms lean and handle this kind of exceptions at the domain model level.

abhishiv commented 8 years ago

Hey gcanti,

I have the same issue and I'm using a transformer as you suggested.

    const transformer =  {
        format: (value) => {
            const ret = value || defaultValue;
            console.log("format", ret);
            return ret;
        },
        parse: (str) => {
          console.log("parse", str);
            return str;
        }
    };

However on first load, in my custom factory component, this.props.value is null, while this.state.value is defaultValue. I deal with it by having an or operator this.props.value || this.state.value . But is it by design?

    "tcomb": "^2.7.0",
    "tcomb-form": "^0.8.2"
gcanti commented 8 years ago

in my custom factory component ... But is it by design?

Yes, you should always read this.state.value