-
Notifications
You must be signed in to change notification settings - Fork 0
Add isOneOf validation.
#83
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ interface UserFormFields { | |
| firstName: string; | ||
| lastName: string; | ||
| email: string; | ||
| tShirtSize: string; | ||
| } | ||
|
|
||
| function getErrorText(formField: FormField, formFields: FormFields, submitting?: boolean) { | ||
|
|
@@ -19,6 +20,8 @@ function getErrorText(formField: FormField, formFields: FormFields, submitting?: | |
| return 'Required'; | ||
| case 'isEmail': | ||
| return 'Invalid email'; | ||
| case 'isOneOf': | ||
| return 'Must be small, medium, or large'; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -42,14 +45,24 @@ const formFieldConfigs: FormFieldConfig[] = [ | |
| isEmail: true, | ||
| getHelperText: getErrorText, | ||
| }, | ||
| { | ||
| name: 'tShirtSize', | ||
| label: 'T-shirt size', | ||
| required: true, | ||
| isOneOf: ['small', 'medium', 'large'], | ||
| getHelperText: getErrorText, | ||
| }, | ||
| ]; | ||
|
|
||
| export class Home extends React.Component { | ||
| render() { | ||
| return ( | ||
| <Page> | ||
| <ClassyForm formFieldConfigs={formFieldConfigs}> | ||
| {({ formFields: { email, firstName, lastName }, onSubmit }: FormsContextContext<UserFormFields>) => { | ||
| {({ | ||
| formFields: { email, firstName, lastName, tshirtSize }, | ||
| onSubmit, | ||
| }: FormsContextContext<UserFormFields>) => { | ||
| return ( | ||
| <React.Fragment> | ||
| <FormInput key={firstName.name} formField={firstName} /> | ||
|
|
@@ -58,6 +71,13 @@ export class Home extends React.Component { | |
|
|
||
| <FormInput key={email.name} formField={email} /> | ||
|
|
||
| <FormInput | ||
| key={tshirtSize.name} | ||
| formField={tshirtSize} | ||
| autoComplete="off" | ||
|
Comment on lines
71
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The destructured field is 🤖 AI Agent Prompt for Cursor/Windsurf
Comment on lines
71
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The destructured field is Affected Locations:
🤖 AI Agent Prompt for Cursor/Windsurf
|
||
| placeholder="small, medium, or large" | ||
|
Comment on lines
72
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The destructured field is Affected Locations:
🤖 AI Agent Prompt for Cursor/Windsurf
Comment on lines
72
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The destructured field is 🤖 AI Agent Prompt for Cursor/Windsurf
Comment on lines
72
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The destructured field is Affected Locations:
🤖 AI Agent Prompt for Cursor/Windsurf
|
||
| /> | ||
|
|
||
| <div className="form-actions"> | ||
| <Button variant="contained" color="primary" type="submit"> | ||
| Register | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,4 +95,7 @@ export const validations = { | |
| minLength(formField: FormField, formFields: FormFields, submitting: boolean, length: number) { | ||
| return !isExisty(formField.value) || isEmpty(formField.value) || formField.value.length >= length; | ||
| }, | ||
| isOneOf(formField: FormField, formFields: FormFields, submitting: boolean, allowedValues: Value[]) { | ||
| return !isExisty(formField.value) || isEmpty(formField.value) || allowedValues.includes(formField.value); | ||
| }, | ||
|
Comment on lines
+98
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The new 🤖 AI Agent Prompt for Cursor/Windsurf
Also ensure
Comment on lines
+98
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: The new 🤖 AI Agent Prompt for Cursor/Windsurf
Also ensure
Comment on lines
+98
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The following block must be added to if (
formFieldConfig.isOneOf &&
!validations.isOneOf(formField, formFields, submitting, formFieldConfig.isOneOf)
) {
errors.push('isOneOf');
}Without this change, the entire feature introduced by this PR has no effect at runtime. |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tshirtSizecasing mismatch causes runtime crashThe field is configured in
formFieldConfigswithname: 'tShirtSize'(capitalS) and declared inUserFormFieldsastShirtSize, but it is destructured here astshirtSize(lowercases). Because the key does not exist informFields,tshirtSizewill beundefinedat runtime. AccessingtshirtSize.nameon line 75 will then throw aTypeError.Fix the destructured name to match the registered field name:
Also update lines 75–79 to reference
tShirtSizeinstead oftshirtSize.