check form validateion

This commit is contained in:
tzw
2024-02-19 17:06:36 +06:30
parent 5ed6ace02f
commit f66895963b
5 changed files with 224 additions and 142 deletions

View File

@@ -16,6 +16,7 @@ class StaffPinEditor extends StatefulWidget {
}
class _StaffPinEditorState extends State<StaffPinEditor> {
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
late User _staff;
bool _enablePinLogin = false;
@@ -110,28 +111,101 @@ class _StaffPinEditorState extends State<StaffPinEditor> {
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor),
body: Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15),
child: ListView(
children: <Widget>[
Column(
children: [
Text(_staff.name ?? "",
style: TextStyle(color: Colors.black, fontSize: 18)),
Text(_staff.fcsID ?? "",
style: TextStyle(color: Colors.black, fontSize: 15)),
],
),
const SizedBox(height: 20),
enablePinBox,
const SizedBox(height: 30),
newPinBox,
SizedBox(height: 30),
confirmPinBox,
SizedBox(height: 30),
saveButton,
SizedBox(height: 30)
],
body: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15),
child: ListView(
children: <Widget>[
Column(
children: [
Text(_staff.name ?? "",
style: TextStyle(color: Colors.black, fontSize: 18)),
Text(_staff.fcsID ?? "",
style: TextStyle(color: Colors.black, fontSize: 15)),
],
),
const SizedBox(height: 20),
enablePinBox,
const SizedBox(height: 30),
FormField(
builder: (FormFieldState<bool> state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
newPinBox,
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: _newPin == '' || _newPin.length < 6
? SizedBox(
height: 20,
child: Text(
state.errorText ?? '',
style: const TextStyle(
color: dangerColor, fontSize: 12),
),
)
: const SizedBox(height: 20),
),
],
);
},
validator: (value) {
if (_newPin == "") {
return 'Enter new PIN';
}
if (_newPin.length < 6) {
return 'New PIN must be 6 digits';
}
return null;
},
),
SizedBox(height: 10),
FormField(
builder: (FormFieldState<bool> state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
confirmPinBox,
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: _confirmPin == '' ||
_confirmPin.length < 6 ||
_confirmPin != _newPin
? SizedBox(
height: 20,
child: Text(
state.errorText ?? '',
style: const TextStyle(
color: dangerColor, fontSize: 12),
),
)
: const SizedBox(height: 20)),
],
);
},
validator: (value) {
if (_confirmPin == "") {
return 'Enter confirm PIN';
}
if (_confirmPin.length < 6) {
return 'Confirm PIN must be 6 digits';
}
if (_confirmPin != _newPin) {
return "Those pins didnt match. Try again.";
}
return null;
},
),
SizedBox(height: 20),
saveButton,
SizedBox(height: 30)
],
),
),
),
));
@@ -150,30 +224,7 @@ class _StaffPinEditorState extends State<StaffPinEditor> {
}
_save() async {
if (_newPin == "") {
showMsgDialog(context, "Error", "Invalid new PIN");
return;
}
if (_newPin.length < 6) {
showMsgDialog(context, "Error", "New PIN must be 6 digits");
return;
}
if (_confirmPin == "") {
showMsgDialog(context, "Error", "Invalid confirm PIN");
return;
}
if (_confirmPin.length < 6) {
showMsgDialog(context, "Error", "Confirm PIN must be 6 digits");
return;
}
if (_confirmPin != _newPin) {
showMsgDialog(context, "Error", "Those pins didnt match. Try again.");
return;
}
if (!_formKey.currentState!.validate()) return;
setState(() {
_isLoading = true;