feat: add form validation

- Add Alert for validation errors
- Check amount and description
- Validate positive number
This commit is contained in:
Dita Aji Pratama 2026-04-18 12:25:10 +07:00
parent fa073d2c9a
commit fafc765b3f

View File

@ -6,6 +6,7 @@ import {
TouchableOpacity,
StyleSheet,
ScrollView,
Alert,
} from 'react-native';
import { COLORS, FONTS } from '../constants/theme';
import { Transaction, CATEGORIES } from '../types';
@ -22,11 +23,20 @@ export const TransactionForm: React.FC<TransactionFormProps> = ({ onAdd }) => {
const [category, setCategory] = useState('lainnya');
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 = {
id: generateId(),
amount: parseInt(amount, 10),
amount: numAmount,
description,
type,
category,