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.

defaultValue support

See original GitHub issue

Hi, there is a right way to set defaultValue to the components? by example if I set

  var Formsy = require('formsy-react');
  var MyAppForm = React.createClass({
    enableButton: function () {
      this.setState({
        canSubmit: true
      });
    },
    disableButton: function () {
      this.setState({
        canSubmit: false
      });
    },
    submit: function (model) {
       console.log(model);
    },
    render: function () {
      return (
        <Formsy.Form onValidSubmit={this.submit} onValid={this.enableButton} onInvalid={this.disableButton}>
          <MyOwnInput name="example" defaultValue="hello" />
          <MyOwnInput name="email" validations="isEmail" validationError="This is not a valid email" required/>
          <button type="submit" disabled={!this.state.canSubmit}>Submit</button>
       </Formsy.Form>
      );
    }
  });

and I have MyOwnInput:

 /** @jsx React.DOM */
  var Formsy = require('formsy-react');
  var MyOwnInput = React.createClass({

    // Add the Formsy Mixin
    mixins: [Formsy.Mixin],

    // setValue() will set the value of the component, which in 
    // turn will validate it and the rest of the form
    changeValue: function (event) {
      this.setValue(event.currentTarget.value);
    },
    render: function () {

      // Set a specific className based on the validation
      // state of this component. showRequired() is true 
      // when the value is empty and the required prop is 
      // passed to the input. showError() is true when the 
      // value typed is invalid
      var className = this.showRequired() ? 'required' : this.showError() ? 'error' : null;

      // An error message is returned ONLY if the component is invalid
      // or the server has returned an error message
      var errorMessage = this.getErrorMessage();

      return (
        <div className={className}>
          <input type="text" onChange={this.changeValue} value={this.getValue()} defaultValue={this.props.defaultValue}/>
          <span>{errorMessage}</span>
        </div>
      );
    }
  });

When render the form, seems everything is ok, because the default value is being displayed, however when I click on submit, the results is

{ 
  "example": null,
  "email": "erik@localhost.com"
}

so, the default value of the input named “example” is being lost 😦

I will appreciate you help. Best Regards.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
elstobcommented, May 25, 2016

👍 on this issue. Working on forms with default value requirements right now.

0reactions
raaymaxcommented, Oct 12, 2016

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

DefaultValue Property - Microsoft Support
Specifies a String value that is automatically entered in a field when a new record is created. For example, in an Addresses table...
Read more >
HTML DOM Input Text defaultValue Property - W3Schools
The defaultValue property sets or returns the default value of a text field. Note: The default value is the value specified in the...
Read more >
defaultValue support async function · Issue #11033 - GitHub
I hope defaultValue can support async function in model defintiton,for example: sequelize.define('foo', { id: { type: Sequelize.
Read more >
defaultValue attribute
defaultValue attribute. The default value specified in an attribute or an element declaration or null if unspecified. If the schema is a W3C...
Read more >
Default parameters - JavaScript - MDN Web Docs
However, it's often useful to set a different default value. This is where default parameters can help. In the following example, ...
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