Skip to content

Commit 5526453

Browse files
(fix) Add quantity > 0 validation to billing form (#648)
* (fix) add quantity > 0 validation to billing form Prevent submission of bill items with zero or empty quantity. Extends `validateSelectedItems` to show an error snackbar and return false when any item's quantity is falsy or less than 1. * review changes * (fix) Remove min={1} from quantity NumberInput min={1} on Carbon's NumberInput prevents the invalid state from surfacing when the user clears or enters a value below 1. Removing it lets the inline invalid/invalidText validation display correctly.
1 parent cb69f71 commit 5526453

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/billing-form/billing-form.workspace.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ const BillingForm: React.FC<Workspace2DefinitionProps<BillingFormProps>> = ({
112112
};
113113

114114
const validateSelectedItems = (): boolean => {
115+
if (selectedItems.length === 0) {
116+
return false;
117+
}
115118
for (const item of selectedItems) {
116119
if (item.availablePaymentMethods && item.availablePaymentMethods.length > 1 && !item.selectedPaymentMethod) {
117120
showSnackbar({
@@ -121,11 +124,17 @@ const BillingForm: React.FC<Workspace2DefinitionProps<BillingFormProps>> = ({
121124
});
122125
return false;
123126
}
127+
if (!item.quantity || item.quantity < 1) {
128+
return false;
129+
}
124130
}
125131
return true;
126132
};
127133

128134
const postBillItems = async () => {
135+
if (isSubmitting || selectedItems.length === 0) {
136+
return;
137+
}
129138
if (!validateSelectedItems()) {
130139
return;
131140
}
@@ -175,9 +184,14 @@ const BillingForm: React.FC<Workspace2DefinitionProps<BillingFormProps>> = ({
175184
}
176185
};
177186

187+
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (event) => {
188+
event.preventDefault();
189+
postBillItems();
190+
};
191+
178192
return (
179193
<Workspace2 title={t('addBillItems', 'Add bill items')} hasUnsavedChanges={selectedItems.length > 0}>
180-
<Form className={styles.form}>
194+
<Form className={styles.form} onSubmit={handleSubmit}>
181195
<div className={styles.grid}>
182196
{isLoading ? (
183197
<InlineLoading description={getCoreTranslation('loading') + '...'} />
@@ -252,10 +266,11 @@ const BillingForm: React.FC<Workspace2DefinitionProps<BillingFormProps>> = ({
252266
disableWheel
253267
hideSteppers
254268
id={`quantity-${item.uuid}`}
255-
min={1}
269+
invalid={!item.quantity || item.quantity < 1}
270+
invalidText={t('quantityMustBeAtLeastOne', 'Quantity must be at least 1')}
256271
onChange={(_, { value }) => {
257272
const number = parseFloat(String(value));
258-
updateQuantity(item.uuid, isNaN(number) ? 1 : number);
273+
updateQuantity(item.uuid, isNaN(number) ? 0 : number);
259274
}}
260275
value={item.quantity}
261276
/>
@@ -292,7 +307,6 @@ const BillingForm: React.FC<Workspace2DefinitionProps<BillingFormProps>> = ({
292307
<Button
293308
className={styles.button}
294309
kind="primary"
295-
onClick={postBillItems}
296310
disabled={isSubmitting || selectedItems.length === 0}
297311
type="submit">
298312
{isSubmitting ? (

0 commit comments

Comments
 (0)