I have made my own form component with render() method looks like this:
render() {
return (
<form onSubmit={this.onSubmit} ref={(c)=>this._form=c}>
{this.props.children}
</form>
)
}
Notice that children are rendered here as {this.props.children}
, so the user can use this component like this:
<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 state of each Input
component (to get its validity) 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);
}