Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Bootstrap Validation use validated input in sub component

I am using react-bootstrap-validation that decorates the react-bootstrap Input tag.

The ValidatedInput requires that it is inside a Form component. When I add my ValidatedInput to a custom sub component I get an error saying it needs to be inside a Form which it is, but I guess it is further down the tree now so can not see the Form.

Is there a way of referencing the parent Form so the ValidatedInput can see the parent.

Looking at the source of the Validation lib I can see that the ValidationInput needs to register to the Form but am not sure how to do this from the sub component.

// Parent render 
render(){
   <Form
   className="fl-form fl-form-inline fl-form-large"
   name="customer-details"
   onValidSubmit={this._handleValidSubmit}
   onInvalidSubmit={this._handleInvalidSubmit}
   validationEvent='onChange'>
    
     <TitleSelect handleChange={this.updateDropDown} value={this.state.form.title} />
     
   </form>
}


// Sub class containing the ValidatedInput
export class TitleSelect extends React.Component {

    static propTypes = {
      handleChange: React.PropTypes.func.isRequired,
      value: React.PropTypes.string.isRequired
    }

    render(){

        return (
          <ValidatedInput
            name="title"
            label='title'
            type='select'
            value={this.props.value}
            onChange={this.props.handleChange}
            groupClassName='form-group input-title'
            wrapperClassName='fl-input-wrapper'
            validate='required'
            errorHelp={{
              required: 'Please select a title.'
            }}>

            <option value="" ></option>
            <option value="Mr">Mr</option>
            <option value="Mrs">Mrs</option>
            <option value="Master">Mstr.</option>
            <option value="Ms">Ms</option>
            <option value="Miss">Miss</option>
            <option value="Reverend">Rev.</option>
            <option value="Doctor">Dr.</option>
            <option value="Professor">Prof.</option>
            <option value="Lord">Lord</option>
            <option value="Lady">Lady</option>
            <option value="Sir">Sir</option>
            <option value="Master">Mstr.</option>
            <option value="Miss">Miss</option>
          </ValidatedInput>
        )
    }
};
like image 415
Will Avatar asked Aug 26 '15 14:08

Will


People also ask

How do you use validator in React?

To validate the field, you pass an object containing the validation function: import React from "react"; import { useField } from "react-form"; function Field({ name }) { const { value = "", meta: { error, isTouched }, getInputProps, } = useField(name, { validate: (value) => { if (!

What is FormControl in React?

Overview. The <FormControl> component renders a form control with Bootstrap styling. The <FormGroup> component wraps a form control with proper spacing, along with support for a label, help text, and validation state. To ensure accessibility, set controlId on <FormGroup> , and use <FormLabel> for the label.


2 Answers

At the moment this is impossible to do. It will be possible in a future release once we get proper parent-based contexts in react and I will migrate the component to contexts. But for now I would recommend to split your render() method to couple of smaller ones and reuse them.

like image 63
Ivan Reshetnikov Avatar answered Sep 30 '22 08:09

Ivan Reshetnikov


Sa @Ваня Решетников said above it's impossible to do it now because of limitations of current design. A solution I went for is this.

  1. Convert subcomponent to plain JS object

    TitleSelect = {
      // move prop types to parent
      renderTitleSelect(){
        ...
      }
    }
    
  2. Add new object as a mixin to parent and render a function

    mixins: [TitleSelect],
    ...
    render() {
      <Form ...>
        // parentheses are important!
        {this.renderTitleSelect()}
      </Form>
    }
    
like image 43
Mike Szyndel Avatar answered Sep 30 '22 08:09

Mike Szyndel