Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active September 3, 2020 02:45
Show Gist options
  • Save kenmori/327b60cec193b77cfea0d06698f3b18c to your computer and use it in GitHub Desktop.
Save kenmori/327b60cec193b77cfea0d06698f3b18c to your computer and use it in GitHub Desktop.
react-formでsubmit前(送信前)に値を確認ごにょごにょしたい(【react】react-formのsubmit前に確認or値を変更したい(日本語))
//これだとそのまま送られてしまう
export class HogeForm extends Component {
constructor(props){
super(props);
}
render(){
return (
<Form>{
formApi => (
<form onSubmit={formApi.submitForm} id='form1'>
<div>
<div><label htmlFor='corporateName' /><span>必須</span></div>
<Text field='corporateName' id='corporateName' />
</div>
<Submit />
</form>
)}
</Form>
)
}
}
/////////////////下のようにする//////////////////////////
import React, {Component} from 'react';
import {Form, Text } from 'react-form';
import 'object-assign';
export class HogeForm extends Component {
constructor(props){
super(props);
this.state = {
corporateName: '',
submits: 0
}
}
submitWrap = (e, formApi) => {
console.log(e, formApi, "ラッパーの中");;
this.setState(Object.assign({}, formApi.values, {submits: formApi.submits}), formApi.submitForm());
console.log("submitForm成功");
}
successValidator = () => {
console.log("successValidator成功")
}
render(){
return (
<Form onSubmit={(submittedValues) => {
console.log(submittedValues, 'submittedValue後');
this.setState({
corporateName: submittedValues.corporateName, submits: 0})
}}
validateSuccess={this.successValidator}
>
{
formApi => (
<form onSubmit={formApi.submitForm} id='form1'>
<div>
<div><label htmlFor='corporateName' /><span>必須</span></div>
<Text field='corporateName' id='corporateName' />
</div>
<button onClick={(e)=> {this.submitWrap(e, formApi)}}>お問い合わせする</button>//ラッパーにformApiを渡す
</form>
)}
</Form>
)
}
}
//ref: https://react-form.js.org/#/
@shimada-lab
Copy link

xx

@shimada-lab
Copy link

xxx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment