-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix: Prevent Formik form submission if native validation fails #4037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Cherryga
wants to merge
2
commits into
jaredpalmer:main
Choose a base branch
from
Cherryga:fix/submit-button-browser-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−32
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * @component TestSubmit | ||
| * @description | ||
| * A styled React Form component using Formik and Yup for form state management and validation. | ||
| * This form includes two fields: Email and Age, both of which are required. | ||
| * | ||
| * Features: | ||
| * - Email validation using Yup's `.email()` and `.required()` rules. | ||
| * - Age validation that ensures: | ||
| * - the input is numeric (transforms empty string to NaN), | ||
| * - it's a whole number (integer), | ||
| * - it's positive, | ||
| * - and it's required. | ||
| * - Inline styles for a clean and modern appearance. | ||
| * - Error messages shown below each field using <ErrorMessage>. | ||
| * - Debug info (`isValid`, `touched`, `errors`) displayed using <pre> block. | ||
| * - Form resets on successful submission. | ||
| * | ||
| * @usage | ||
| * <TestSubmit /> | ||
| * | ||
| * @note | ||
| * - Age input uses Yup's `.transform()` to handle empty strings correctly, ensuring validation works with edge cases like decimal input. | ||
| * - All logic is contained in a single component for simplicity. | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| import React from 'react'; | ||
| import { Formik, Form, Field, ErrorMessage } from 'formik'; | ||
| import * as Yup from 'yup'; | ||
|
|
||
| // Validation Schema | ||
| const validationSchema = Yup.object({ | ||
| email: Yup.string() | ||
| .email('Invalid email format') | ||
| .required('Email is required'), | ||
| age: Yup.number() | ||
| .transform((value, originalValue) => | ||
| String(originalValue).trim() === '' ? NaN : Number(originalValue) | ||
| ) | ||
| .typeError('Age must be a number') | ||
| .integer('Age must be a whole number') | ||
| .positive('Age must be a positive number') | ||
| .required('Age is required'), | ||
| }); | ||
|
|
||
| const styles = { | ||
| formContainer: { | ||
| maxWidth: '400px', | ||
| margin: '40px auto', | ||
| padding: '24px', | ||
| border: '1px solid #ccc', | ||
| borderRadius: '10px', | ||
| fontFamily: 'Arial, sans-serif', | ||
| boxShadow: '0 0 10px rgba(0,0,0,0.1)', | ||
| backgroundColor: '#f9f9f9', | ||
| }, | ||
| fieldGroup: { | ||
| marginBottom: '16px', | ||
| }, | ||
| label: { | ||
| display: 'block', | ||
| marginBottom: '6px', | ||
| fontWeight: 'bold', | ||
| }, | ||
| input: { | ||
| width: '100%', | ||
| padding: '8px', | ||
| borderRadius: '4px', | ||
| border: '1px solid #ccc', | ||
| }, | ||
| errorText: { | ||
| color: 'red', | ||
| fontSize: '12px', | ||
| marginTop: '4px', | ||
| }, | ||
| submitButton: { | ||
| padding: '10px 20px', | ||
| backgroundColor: '#4caf50', | ||
| color: 'white', | ||
| border: 'none', | ||
| borderRadius: '6px', | ||
| cursor: 'pointer', | ||
| }, | ||
| }; | ||
|
|
||
| const TestSubmit = () => { | ||
| return ( | ||
| <Formik | ||
| initialValues={{ email: '', age: '' }} | ||
| validationSchema={validationSchema} | ||
| onSubmit={(values, { resetForm }) => { | ||
| console.log('✅ Form submitted successfully:', values); | ||
| alert('Form submitted successfully'); | ||
| resetForm(); | ||
| }} | ||
| > | ||
| {({ isValid, touched, errors }) => ( | ||
| <Form style={styles.formContainer} noValidate> | ||
| <div style={styles.fieldGroup}> | ||
| <label style={styles.label}>Email:</label> | ||
| <Field name="email" type="email" style={styles.input} /> | ||
| <ErrorMessage name="email" component="div" style={styles.errorText} /> | ||
| </div> | ||
|
|
||
| <div style={styles.fieldGroup}> | ||
| <label style={styles.label}>Age:</label> | ||
| <Field name="age" type="number" style={styles.input} /> | ||
| <ErrorMessage name="age" component="div" style={styles.errorText} /> | ||
| </div> | ||
|
|
||
| <button type="submit" style={styles.submitButton}> | ||
| Submit | ||
| </button> | ||
|
|
||
| {/* Debug output */} | ||
| <pre>{JSON.stringify({ isValid, touched, errors }, null, 2)}</pre> | ||
| </Form> | ||
| )} | ||
| </Formik> | ||
| ); | ||
| }; | ||
|
|
||
| export default TestSubmit; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.