feat: add form validation
- Add Alert for validation errors - Check amount and description - Validate positive number
This commit is contained in:
parent
fa073d2c9a
commit
fafc765b3f
@ -6,6 +6,7 @@ import {
|
|||||||
TouchableOpacity,
|
TouchableOpacity,
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
ScrollView,
|
ScrollView,
|
||||||
|
Alert,
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
import { COLORS, FONTS } from '../constants/theme';
|
import { COLORS, FONTS } from '../constants/theme';
|
||||||
import { Transaction, CATEGORIES } from '../types';
|
import { Transaction, CATEGORIES } from '../types';
|
||||||
@ -22,11 +23,20 @@ export const TransactionForm: React.FC<TransactionFormProps> = ({ onAdd }) => {
|
|||||||
const [category, setCategory] = useState('lainnya');
|
const [category, setCategory] = useState('lainnya');
|
||||||
|
|
||||||
const handleSubmit = () => {
|
const handleSubmit = () => {
|
||||||
if (!amount || !description) return;
|
if (!amount || !description) {
|
||||||
|
Alert.alert('Error', 'Mohon isi jumlah dan deskripsi');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const numAmount = parseInt(amount, 10);
|
||||||
|
if (isNaN(numAmount) || numAmount <= 0) {
|
||||||
|
Alert.alert('Error', 'Jumlah harus angka positif');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const transaction: Transaction = {
|
const transaction: Transaction = {
|
||||||
id: generateId(),
|
id: generateId(),
|
||||||
amount: parseInt(amount, 10),
|
amount: numAmount,
|
||||||
description,
|
description,
|
||||||
type,
|
type,
|
||||||
category,
|
category,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user