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

@@ -26,6 +26,7 @@ class FcsShipmentEditor extends StatefulWidget {
}
class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
final _formKey = GlobalKey<FormState>();
var dateFormatter = new DateFormat('dd MMM yyyy');
TextEditingController _shipmentNumberController = new TextEditingController();
TextEditingController _cutoffDateController = new TextEditingController();
@@ -111,63 +112,86 @@ class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
Navigator.of(context).pop();
}
}),
body: ListView(
padding: const EdgeInsets.only(left: 10, right: 10),
children: <Widget>[
InputText(
labelTextKey: "FCSshipment.number",
iconData: Ionicons.ios_airplane,
controller: _shipmentNumberController,
),
InputDate(
labelTextKey: "FCSshipment.cutoff_date",
iconData: Icons.date_range,
controller: _cutoffDateController,
),
InputDate(
labelTextKey: "FCSshipment.ETA",
iconData: Icons.date_range,
controller: _arrivalDateController,
),
DropdownButtonFormField(
value: _currentShipmentType == "" ? null : _currentShipmentType,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor)),
fillColor: Colors.white,
labelStyle: languageModel.isEng
? newLabelStyle(color: Colors.black54, fontSize: 20)
: newLabelStyleMM(color: Colors.black54, fontSize: 20),
labelText: AppTranslations.of(context)!
.text('FCSshipment.shipment_type'),
icon: Icon(Ionicons.ios_airplane, color: primaryColor)),
items: mainModel.setting!.shipmentTypes
.map((e) => DropdownMenuItem(child: Text(e), value: e))
.toList(),
onChanged: (String? selected) => {
setState(() {
_currentShipmentType = selected;
})
},
),
InputText(
labelTextKey: 'FCSshipment.consignee',
iconData: Icons.work,
controller: _consigneeController),
InputText(
labelTextKey: 'FCSshipment.port_of_loading',
iconData: FontAwesomeIcons.ship,
controller: _portController),
InputText(
labelTextKey: 'FCSshipment.final_destination',
iconData: MaterialCommunityIcons.location_enter,
controller: _destinationController),
SizedBox(height: 20),
_isNew ? createBtn : updateBtn,
SizedBox(height: 15)
]),
body: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.only(left: 10, right: 10),
children: <Widget>[
InputText(
labelTextKey: "FCSshipment.number",
iconData: Ionicons.ios_airplane,
controller: _shipmentNumberController,
validator: (value) {
if (value!.isEmpty) {
return "Enter shipment number";
}
return null;
},
),
InputDate(
labelTextKey: "FCSshipment.cutoff_date",
iconData: Icons.date_range,
controller: _cutoffDateController,
validator: (value) {
if (value!.isEmpty) {
return "Select cutoff date";
}
return null;
},
),
InputDate(
labelTextKey: "FCSshipment.ETA",
iconData: Icons.date_range,
controller: _arrivalDateController,
validator: (value) {
if (value!.isEmpty) {
return "Select ETA date";
}
return null;
},
),
DropdownButtonFormField(
value:
_currentShipmentType == "" ? null : _currentShipmentType,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor)),
fillColor: Colors.white,
labelStyle: languageModel.isEng
? newLabelStyle(color: Colors.black54, fontSize: 20)
: newLabelStyleMM(
color: Colors.black54, fontSize: 20),
labelText: AppTranslations.of(context)!
.text('FCSshipment.shipment_type'),
icon: Icon(Ionicons.ios_airplane, color: primaryColor)),
items: mainModel.setting!.shipmentTypes
.map((e) => DropdownMenuItem(child: Text(e), value: e))
.toList(),
onChanged: (String? selected) => {
setState(() {
_currentShipmentType = selected;
})
},
),
InputText(
labelTextKey: 'FCSshipment.consignee',
iconData: Icons.work,
controller: _consigneeController),
InputText(
labelTextKey: 'FCSshipment.port_of_loading',
iconData: FontAwesomeIcons.ship,
controller: _portController),
InputText(
labelTextKey: 'FCSshipment.final_destination',
iconData: MaterialCommunityIcons.location_enter,
controller: _destinationController),
SizedBox(height: 20),
_isNew ? createBtn : updateBtn,
SizedBox(height: 15)
]),
),
),
);
}
@@ -180,45 +204,20 @@ class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
fcsShipment.consignee = _consigneeController.text;
fcsShipment.port = _portController.text;
fcsShipment.destination = _destinationController.text;
try {
var cutoffDate = _cutoffDateController.text;
var arrivalDate = _arrivalDateController.text;
fcsShipment.cutoffDate =
(cutoffDate == "" ? null : dateFormatter.parse(cutoffDate))!;
fcsShipment.arrivalDate =
(arrivalDate == "" ? null : dateFormatter.parse(arrivalDate))!;
} catch (e) {
// showMsgDialog(context, "Error", e.toString()); // shold never happen
}
var cutoffDate = _cutoffDateController.text;
var arrivalDate = _arrivalDateController.text;
fcsShipment.cutoffDate =
(cutoffDate == "" ? null : dateFormatter.parse(cutoffDate))!;
fcsShipment.arrivalDate =
(arrivalDate == "" ? null : dateFormatter.parse(arrivalDate))!;
return fcsShipment;
}
Future<bool> _validate(FcsShipment fcsShipment) async {
if (fcsShipment.shipmentNumber == "") {
await showMsgDialog(context, "Error", "Invalid shipment number!");
return false;
}
if (fcsShipment.shipType == null) {
await showMsgDialog(context, "Error", "Invalid shipment type!");
return false;
}
if (fcsShipment.cutoffDate == null) {
await showMsgDialog(context, "Error", "Invalid cutoff date!");
return false;
}
if (fcsShipment.arrivalDate == null) {
await showMsgDialog(context, "Error", "Invalid ETA date!");
return false;
}
return true;
}
Future<void> _create() async {
if (!_formKey.currentState!.validate()) return;
FcsShipment fcsShipment = _getPayload();
bool valid = await _validate(fcsShipment);
if (!valid) {
return;
}
setState(() {
_isLoading = true;
});
@@ -236,11 +235,9 @@ class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
}
Future<void> _update() async {
if (!_formKey.currentState!.validate()) return;
FcsShipment fcsShipment = _getPayload();
bool valid = await _validate(fcsShipment);
if (!valid) {
return;
}
setState(() {
_isLoading = true;
});