How to use formik along with a stateful react component
up vote
1
down vote
favorite
I have just started learning reactjs. I came across formik and yup while searching for form validation. Most of the examples that i have gone through so far are talking about stateless components. So I would like to know how can I use formik and yup in a stateful component?
I have got a simple employee details form with email,first name, last name, phone and city. The data for the form is loaded from the database. Once the data is loaded, then user can edit and save it to db. Could you please help me to add formik and yup into my project by adding validation for all my above fields? My Code is also present here https://codesandbox.io/s/l26rqyx6j7
My form is given below EmpJobDetailsForm.js
import React from "react";
import { getEmpDetails } from "./APIUtils";
import { withFormik, Form, Field } from "formik";
import Yup from "yup";
class EmpJobDetailsForm extends React.Component {
state = {
data: {}
};
componentWillMount() {
this.getData();
}
getData = () => {
let response = getEmpDetails();
this.setState({ data: response });
};
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input type="email" name="email" value={this.state.data.email} />
</div>
<div>
<label htmlFor="firstName">First Name</label>
<input
type="text"
name="firstName"
value={this.state.data.firstName}
/>
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<input
type="text"
name="lastName"
value={this.state.data.lastName}
/>
</div>
<div>
<label htmlFor="phone">Phone</label>
<input type="text" name="phone" value={this.state.data.phone} />
</div>
<div>
<label htmlFor="city">City</label>
<input type="text" name="city" value={this.state.data.city} />
</div>
<div>
<button type="button" className="outline">
Reset
</button>
<button type="submit">Submit</button>
</div>
</form>
</div>
);
}
}
export default EmpJobDetailsForm;
I have a simple APIUtils.js. It is very basic and i will add code to fetch data from db here.
export function getEmpDetails() {
var employeeData = {};
employeeData.email = "test@gmail.com";
employeeData.firstName = "Luis";
employeeData.middleName = "John";
employeeData.lastName = "Nakano";
employeeData.phone = "1112223333";
employeeData.city = "Dallas";
return employeeData;
}
Appreciate your help.
Thanks
reactjs formik yup
add a comment |
up vote
1
down vote
favorite
I have just started learning reactjs. I came across formik and yup while searching for form validation. Most of the examples that i have gone through so far are talking about stateless components. So I would like to know how can I use formik and yup in a stateful component?
I have got a simple employee details form with email,first name, last name, phone and city. The data for the form is loaded from the database. Once the data is loaded, then user can edit and save it to db. Could you please help me to add formik and yup into my project by adding validation for all my above fields? My Code is also present here https://codesandbox.io/s/l26rqyx6j7
My form is given below EmpJobDetailsForm.js
import React from "react";
import { getEmpDetails } from "./APIUtils";
import { withFormik, Form, Field } from "formik";
import Yup from "yup";
class EmpJobDetailsForm extends React.Component {
state = {
data: {}
};
componentWillMount() {
this.getData();
}
getData = () => {
let response = getEmpDetails();
this.setState({ data: response });
};
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input type="email" name="email" value={this.state.data.email} />
</div>
<div>
<label htmlFor="firstName">First Name</label>
<input
type="text"
name="firstName"
value={this.state.data.firstName}
/>
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<input
type="text"
name="lastName"
value={this.state.data.lastName}
/>
</div>
<div>
<label htmlFor="phone">Phone</label>
<input type="text" name="phone" value={this.state.data.phone} />
</div>
<div>
<label htmlFor="city">City</label>
<input type="text" name="city" value={this.state.data.city} />
</div>
<div>
<button type="button" className="outline">
Reset
</button>
<button type="submit">Submit</button>
</div>
</form>
</div>
);
}
}
export default EmpJobDetailsForm;
I have a simple APIUtils.js. It is very basic and i will add code to fetch data from db here.
export function getEmpDetails() {
var employeeData = {};
employeeData.email = "test@gmail.com";
employeeData.firstName = "Luis";
employeeData.middleName = "John";
employeeData.lastName = "Nakano";
employeeData.phone = "1112223333";
employeeData.city = "Dallas";
return employeeData;
}
Appreciate your help.
Thanks
reactjs formik yup
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have just started learning reactjs. I came across formik and yup while searching for form validation. Most of the examples that i have gone through so far are talking about stateless components. So I would like to know how can I use formik and yup in a stateful component?
I have got a simple employee details form with email,first name, last name, phone and city. The data for the form is loaded from the database. Once the data is loaded, then user can edit and save it to db. Could you please help me to add formik and yup into my project by adding validation for all my above fields? My Code is also present here https://codesandbox.io/s/l26rqyx6j7
My form is given below EmpJobDetailsForm.js
import React from "react";
import { getEmpDetails } from "./APIUtils";
import { withFormik, Form, Field } from "formik";
import Yup from "yup";
class EmpJobDetailsForm extends React.Component {
state = {
data: {}
};
componentWillMount() {
this.getData();
}
getData = () => {
let response = getEmpDetails();
this.setState({ data: response });
};
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input type="email" name="email" value={this.state.data.email} />
</div>
<div>
<label htmlFor="firstName">First Name</label>
<input
type="text"
name="firstName"
value={this.state.data.firstName}
/>
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<input
type="text"
name="lastName"
value={this.state.data.lastName}
/>
</div>
<div>
<label htmlFor="phone">Phone</label>
<input type="text" name="phone" value={this.state.data.phone} />
</div>
<div>
<label htmlFor="city">City</label>
<input type="text" name="city" value={this.state.data.city} />
</div>
<div>
<button type="button" className="outline">
Reset
</button>
<button type="submit">Submit</button>
</div>
</form>
</div>
);
}
}
export default EmpJobDetailsForm;
I have a simple APIUtils.js. It is very basic and i will add code to fetch data from db here.
export function getEmpDetails() {
var employeeData = {};
employeeData.email = "test@gmail.com";
employeeData.firstName = "Luis";
employeeData.middleName = "John";
employeeData.lastName = "Nakano";
employeeData.phone = "1112223333";
employeeData.city = "Dallas";
return employeeData;
}
Appreciate your help.
Thanks
reactjs formik yup
I have just started learning reactjs. I came across formik and yup while searching for form validation. Most of the examples that i have gone through so far are talking about stateless components. So I would like to know how can I use formik and yup in a stateful component?
I have got a simple employee details form with email,first name, last name, phone and city. The data for the form is loaded from the database. Once the data is loaded, then user can edit and save it to db. Could you please help me to add formik and yup into my project by adding validation for all my above fields? My Code is also present here https://codesandbox.io/s/l26rqyx6j7
My form is given below EmpJobDetailsForm.js
import React from "react";
import { getEmpDetails } from "./APIUtils";
import { withFormik, Form, Field } from "formik";
import Yup from "yup";
class EmpJobDetailsForm extends React.Component {
state = {
data: {}
};
componentWillMount() {
this.getData();
}
getData = () => {
let response = getEmpDetails();
this.setState({ data: response });
};
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input type="email" name="email" value={this.state.data.email} />
</div>
<div>
<label htmlFor="firstName">First Name</label>
<input
type="text"
name="firstName"
value={this.state.data.firstName}
/>
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<input
type="text"
name="lastName"
value={this.state.data.lastName}
/>
</div>
<div>
<label htmlFor="phone">Phone</label>
<input type="text" name="phone" value={this.state.data.phone} />
</div>
<div>
<label htmlFor="city">City</label>
<input type="text" name="city" value={this.state.data.city} />
</div>
<div>
<button type="button" className="outline">
Reset
</button>
<button type="submit">Submit</button>
</div>
</form>
</div>
);
}
}
export default EmpJobDetailsForm;
I have a simple APIUtils.js. It is very basic and i will add code to fetch data from db here.
export function getEmpDetails() {
var employeeData = {};
employeeData.email = "test@gmail.com";
employeeData.firstName = "Luis";
employeeData.middleName = "John";
employeeData.lastName = "Nakano";
employeeData.phone = "1112223333";
employeeData.city = "Dallas";
return employeeData;
}
Appreciate your help.
Thanks
reactjs formik yup
reactjs formik yup
asked Nov 5 at 2:08
Shawn
284
284
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Formik is stateful, it's first goal is to "Getting values in and out of form state" (without using Redux-Form
).
A cleaner way to do this would be to use a Formik
form and to add your fetched data as initalValues
(but first remove all the value
attributes from your current inputs
).
You can also take a look at the 'withFormik' higher-order component and particularly the mapsPropsToValues option.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Formik is stateful, it's first goal is to "Getting values in and out of form state" (without using Redux-Form
).
A cleaner way to do this would be to use a Formik
form and to add your fetched data as initalValues
(but first remove all the value
attributes from your current inputs
).
You can also take a look at the 'withFormik' higher-order component and particularly the mapsPropsToValues option.
add a comment |
up vote
0
down vote
Formik is stateful, it's first goal is to "Getting values in and out of form state" (without using Redux-Form
).
A cleaner way to do this would be to use a Formik
form and to add your fetched data as initalValues
(but first remove all the value
attributes from your current inputs
).
You can also take a look at the 'withFormik' higher-order component and particularly the mapsPropsToValues option.
add a comment |
up vote
0
down vote
up vote
0
down vote
Formik is stateful, it's first goal is to "Getting values in and out of form state" (without using Redux-Form
).
A cleaner way to do this would be to use a Formik
form and to add your fetched data as initalValues
(but first remove all the value
attributes from your current inputs
).
You can also take a look at the 'withFormik' higher-order component and particularly the mapsPropsToValues option.
Formik is stateful, it's first goal is to "Getting values in and out of form state" (without using Redux-Form
).
A cleaner way to do this would be to use a Formik
form and to add your fetched data as initalValues
(but first remove all the value
attributes from your current inputs
).
You can also take a look at the 'withFormik' higher-order component and particularly the mapsPropsToValues option.
answered 2 days ago
tcollart
78921024
78921024
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147433%2fhow-to-use-formik-along-with-a-stateful-react-component%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password