I created my own form component with the render () method, which looks like this:
render() { return ( <form onSubmit={this.onSubmit} ref={(c)=>this._form=c}> {this.props.children} </form> ) }
Please note that children appear here as {this.props.children} , so the user can use this component as follows:
<Form onSubmit={this.submit} > <Input name={"name"} id="name" labelName="Ime" placeholder="Unesite ime" type="text" > <Validation rule="required" message="Ovo je obavezno polje"/> </Input> <Input name={"email"} id="email" labelName="Email" placeholder="Unesite email adresu" type="text" > <Validation rule="required" message="Ovo je obavezno polje"/> <Validation rule="email" message="Ovo je nije valjana email adresa"/> </Input> <button type="submit" value="Pošalji" >Pošalji</button> </Form>
I would like to check the status of each Input component (to get its authenticity) inside onSubmitMethod() .
checkValidity() { var sefl = this; this.props.children.map((child) => { if (child.type.name === "Input") { //How to get state of child here } }); } onSubmit(event) { event.preventDefault(); var obj = serialize(this._form, { hash: true }); const validityOfForm = true; //hardcoded for now this.checkValidity(); this.props.onSubmit(obj, validityOfForm); }
source share