mirror of
https://github.com/acedanger/finance.git
synced 2026-03-25 01:41:51 -07:00
feat: enhance form validation in AddTransactionForm and improve error handling in transactions API
This commit is contained in:
@@ -153,7 +153,7 @@
|
||||
|
||||
// Check required fields
|
||||
const description = formData.get('description') as string;
|
||||
const amount = formData.get('amount');
|
||||
const amount = formData.get('amount') as string;
|
||||
const date = formData.get('date');
|
||||
|
||||
if (!description || description.trim().length < 2) {
|
||||
@@ -162,10 +162,22 @@
|
||||
|
||||
if (!amount) {
|
||||
errors.push('Amount is required');
|
||||
} else {
|
||||
const amountNum = parseFloat(amount);
|
||||
if (isNaN(amountNum)) {
|
||||
errors.push('Amount must be a valid number');
|
||||
} else if (amountNum === 0) {
|
||||
errors.push('Amount cannot be zero');
|
||||
}
|
||||
}
|
||||
|
||||
if (!date) {
|
||||
errors.push('Date is required');
|
||||
} else {
|
||||
const dateObj = new Date(date as string);
|
||||
if (isNaN(dateObj.getTime())) {
|
||||
errors.push('Invalid date format');
|
||||
}
|
||||
}
|
||||
|
||||
return errors.length > 0 ? errors : null;
|
||||
@@ -226,7 +238,7 @@
|
||||
amount: amount
|
||||
};
|
||||
|
||||
const transactionId = formData.get('id');
|
||||
const transactionId = txnIdInput.value;
|
||||
const method = transactionId ? 'PUT' : 'POST';
|
||||
const url = transactionId
|
||||
? `/api/transactions/${transactionId}`
|
||||
@@ -250,14 +262,17 @@
|
||||
|
||||
// Update UI
|
||||
const eventName = isEditMode ? 'transactionUpdated' : 'transactionCreated';
|
||||
const event = new CustomEvent(eventName, {
|
||||
document.dispatchEvent(new CustomEvent(eventName, {
|
||||
detail: { transaction: savedTransaction }
|
||||
});
|
||||
document.dispatchEvent(event);
|
||||
}));
|
||||
|
||||
// Clear and collapse form
|
||||
clearForm();
|
||||
form.classList.add('collapsed');
|
||||
if (toggleBtn) {
|
||||
toggleBtn.setAttribute('aria-expanded', 'false');
|
||||
toggleBtn.textContent = 'Add Transaction +';
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
showError(error instanceof Error ? error.message : 'An unexpected error occurred');
|
||||
|
||||
Reference in New Issue
Block a user