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.

FormItem doesn't detect control input inside

See original GitHub issue

Environment

  • antd version: 2.7.4
  • OS and its version: OS X 10.12.3
  • Browser and its version: Chrome 56

What did you do? Please provide steps to re-produce your problem.

We wrap our form fields into their own component which contain their validation logic. Example

UsernameInput.js

class UsernameInput extends Component {
  render() {
    return this.props.form.getFieldDecorator(this.props.id, {
      rules: [...]
    })(InputComponent);
  }
}

And we want to use them in our forms like this

// imports here

class CustomForm {
  render() {
    return (<Form>
      <Form.Item>
        <UsernameInput form={this.props.form} />
      </Form.Item>
    </Form>);
  }
}

export default Form.create()(CustomForm);

This doesn’t work because FormItem.getControls doesn’t detect the component as a control. Just sees the wrapper as UsernameInput.

What do you expected?

FormItem to detect UsernameInput and work as any input control. Show error messages, feedback, helps, etc.

What happen?

FormItem doesn’t find the control inside.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
gaastonsrcommented, Mar 29, 2017

We solved this by calling the component as a function

CustomForm.js

// imports here

class CustomForm {
  render() {
    return (<Form>
      <Form.Item>
        {UsernameInput({ form: this.props.form })}
      </Form.Item>
    </Form>);
  }
}

export default Form.create()(CustomForm);

But if you’re open to ideas, I think a possible solution would be to define a context in the FormItem and the Input can register themselves when it’s present. Example

FormItem.js

class FormItem extends Component {
  static childContextTypes = {
    registerControl: PropTypes.func
  };

  getChildContext() {
    return { registerControl: this.registerControl }; 
  }

  registerControl(control) {
    this.controls.push(control);
  }
}

Input.js

class Input extends Component {
  static contextTypes = {
    registerControl: PropTypes.func
  };

  componentDidMount() {
    if (this.context.registerControl) {
      this.registerControl(this);
    }
  }
}
1reaction
tomasfranciscocommented, Aug 2, 2018

@gaastonsr Your solution help me a lot!

Is this is a still valid issue? The context suggestion sounds good to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ant.design does not detect custom input component in Form ...
You need to pass to your custom component all props, because Form.Item pass to there onChange and value props function CustomInput({size ...
Read more >
Controller | React Hook Form - Simple React forms validation
This simplifies integrating with external controlled components with non-standard prop names. Provides onChange , onBlur , name , ref and value to the...
Read more >
Form - Ant Design
This demo shows how to use Form.Item with multiple controls. <Form.Item name="field" /> will only bind the control(Input/Select) which ...
Read more >
ASP.NET Core Blazor forms and input components
Learn how to use forms with field validation and built-in input components in Blazor.
Read more >
Using Material UI with React Hook Form - LogRocket Blog
We have already seen that we need these two things to control almost any input. Refactoring our form. So let's see if the...
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