-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve expense adding form validation
This should prevent form submission and give sensible feedback to the users when entering an amount that can't be parsed as a number Also checks that at least one participant is involved
- Loading branch information
Showing
5 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
import JsonForm from './JsonForm.js' | ||
|
||
export default class AddExpenseForm extends JsonForm { | ||
validate(newExpense) { | ||
if (Number.isNaN(Number(newExpense.amount))) { | ||
return [{name: 'amount', error: 'Invalid amount. Please enter a valid number'}] | ||
} | ||
|
||
if (!newExpense.participants || !newExpense.participants.length) { | ||
return [{error: 'Please choose who this expense applies to'}] | ||
} | ||
|
||
return [] | ||
} | ||
|
||
format(newExpense) { | ||
return Object.assign({}, newExpense, {amount: Number(newExpense.amount)}) | ||
} | ||
} |
This file contains 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 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 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