problem with init value and enum
See original GitHub issueI
I have a form containing some fields of type enum and I want to be able to populate the form with previously stored values from my server, when they exist, and to keep the fields with default empty (-) value when not the case. The problem is that, if I don’t provide a complete initial structure to init the form, fields of type enum can’t validate until I changed them to a different value.
Currently, the code I have (and that work) is this:
constructor(props) {
super(props);
this.app = this.props.app;
this.state = {
loading: true,
saving: false,
initialvalue: {
name:"",
gender: "M",
birthdate: new Date(),
address: "",
country: "FR",
langs: []
},
}
}
componentWillMount() {
this.setState({loading: true});
this.app.service('users').get(this.app.get('user')._id).then((result) => { <-- I retrieve previously stored data from my server. If they exist
this.setState({
initialvalue: {
name: result["name"],
gender: result["gender"],
birthdate: new Date(result["birthdate"]),
address: result["address"],
country: result["country"],
langs: result["langs"]
}
});
this.setState({loading: false});
console.log('value changed');
}).catch((err) => {
console.log('err');
console.log(err);
this.setState({initialvalue: {}});
this.setState({loading: false});
});
render() {
return (
<TouchableWithoutFeedback onPress={this._dismissKeyboard.bind(this)}>
<ScrollView>
<View style={baseStyles.container}>
<Form
ref="form"
type={Person}
options={options}
value={this.state.initialvalue}
/>
The problem with this code is that, if some fields do not yet exists in my server, these fields will be populated by the default values defined in the constructor (for example, the gender would be “M”). I tried to remove the initialvalue from the constructor or to to set it as initialvalue:{} but, in that case, the form bug and refuse to validate until I manually change the value of enum fields to a value different to the one retrieved from my server.
So how can I initialize a form with some value, when they exist in my server, and keep it blank if not the case?
My conf:
- tcomb-form-native v0.6.0
- react-native v0.30
Issue Analytics
- State:
- Created 7 years ago
- Comments:15 (1 by maintainers)
Top GitHub Comments
I think there’s a validation error when changing the
Select
to any value. I need more time to dig deeper into the issue! Thanks for reporting @ramsestom.Will test later today. Thanks.