question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

problem with init value and enum

See original GitHub issue

I

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:open
  • Created 7 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
alvarombcommented, Aug 11, 2016

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.

0reactions
alvarombcommented, Aug 8, 2016

Will test later today. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't initialize an enum with a value from its underlying type
cpp:20:23: error: cannot convert 'int' to 'Color' in initialization). I was going through the c++ book it says it's OK to initialize the...
Read more >
InitValue for enums no longer working · Issue #6106 - GitHub
Describe the bug A table field of type enum cannot have an InitialValue To Reproduce Given: enum with some members. a table with...
Read more >
CA1008: Enums should have zero value (code analysis) - .NET
A non-flags-attributed enumeration should define a member that has the value of zero so that the default value is a valid value of...
Read more >
Enumeration declaration - cppreference.com
Each enumerator is associated with a value of the underlying type. When initializers are provided in the enumerator-list, the values of ...
Read more >
Quiz yourself: Initializing enums in Java code - Oracle Blogs
This question investigates the initialization of enums, which are a data type that allows for a variable to be a set of predefined...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found