From 791f15f6d3889203ff1d1f3d7e11d3a2d3b708ef Mon Sep 17 00:00:00 2001 From: sma Date: Fri, 1 Mar 2024 17:04:51 +0630 Subject: [PATCH] add validation in faq and discount page --- lib/pages/discount/discount_editor.dart | 87 ++++++++++++++++--------- lib/pages/faq/faq_edit_page.dart | 19 ++++-- 2 files changed, 70 insertions(+), 36 deletions(-) diff --git a/lib/pages/discount/discount_editor.dart b/lib/pages/discount/discount_editor.dart index f18a059..bf20e46 100644 --- a/lib/pages/discount/discount_editor.dart +++ b/lib/pages/discount/discount_editor.dart @@ -30,6 +30,7 @@ class _DiscountEditorState extends State { bool _isNew = false; String customerName = ''; String customerId = ''; + final _discountFormKey = GlobalKey(); @override void initState() { @@ -49,14 +50,30 @@ class _DiscountEditorState extends State { @override Widget build(BuildContext context) { final codeBox = InputText( - labelTextKey: 'discount.code', - iconData: FontAwesomeIcons.algolia, - controller: _codeController); + labelTextKey: 'discount.code', + iconData: FontAwesomeIcons.algolia, + controller: _codeController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert code"; + } + return null; + }, + ); final amountBox = InputText( - labelTextKey: 'discount.amount', - iconData: FontAwesomeIcons.moneyBill, - controller: _amountController); + labelTextKey: 'discount.amount', + iconData: FontAwesomeIcons.moneyBill, + controller: _amountController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert amount"; + } + return null; + }, + ); final statusBox = DisplayText( text: _statusController.text, @@ -100,39 +117,45 @@ class _DiscountEditorState extends State { onPressed: _delete) ], ), - body: Padding( - padding: const EdgeInsets.all(8.0), - child: Column( - children: [ - Expanded( - child: ListView( - children: [ - codeBox, - amountBox, - SizedBox(height: 7), - customerBox, - SizedBox(height: 7), - widget.discount == null - ? Container() - : Container( - padding: EdgeInsets.only(top: 5), - child: statusBox, - ), - ], + body: Form( + key: _discountFormKey, + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + children: [ + Expanded( + child: ListView( + children: [ + codeBox, + amountBox, + SizedBox(height: 7), + customerBox, + SizedBox(height: 7), + widget.discount == null + ? Container() + : Container( + padding: EdgeInsets.only(top: 5), + child: statusBox, + ), + ], + ), ), - ), - fcsButton(context, getLocalString(context, "btn.save"), - callack: _save), - SizedBox( - height: 30, - ) - ], + fcsButton(context, getLocalString(context, "btn.save"), + callack: _save), + SizedBox( + height: 30, + ) + ], + ), ), )), ); } _save() async { + if (!_discountFormKey.currentState!.validate()) { + return null; + } setState(() { _isLoading = true; }); diff --git a/lib/pages/faq/faq_edit_page.dart b/lib/pages/faq/faq_edit_page.dart index b0da35d..a701c10 100644 --- a/lib/pages/faq/faq_edit_page.dart +++ b/lib/pages/faq/faq_edit_page.dart @@ -29,7 +29,7 @@ class _FAQEditorState extends State { TextEditingController _pageLabelEng = new TextEditingController(); TextEditingController _pageLabelMm = new TextEditingController(); - final _formKey = GlobalKey(); + final _faqFormKey = GlobalKey(); bool _isLoading = false; bool _isNew = false; String _pageLink = info; @@ -57,9 +57,16 @@ class _FAQEditorState extends State { final snBox = InputText( controller: _sn, labelTextKey: "faq.edit.sn", - maxLines: 1, withBorder: false, textInputType: TextInputType.number, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty || value.length==0) { + return "Please insert S/N"; + } + + return null; + }, ); final questionEngBox = InputText( @@ -151,7 +158,7 @@ class _FAQEditorState extends State { ], ), body: Form( - key: _formKey, + key: _faqFormKey, child: Padding( padding: EdgeInsets.only(left: 24.0, right: 24.0), child: ListView( @@ -189,6 +196,10 @@ class _FAQEditorState extends State { } _save() async { + if (!_faqFormKey.currentState!.validate()) { + return null; + } + setState(() { _isLoading = true; }); @@ -212,7 +223,7 @@ class _FAQEditorState extends State { } Navigator.pop(context); } catch (e) { - showMsgDialog(context, "Error", e.toString()); + showMsgDialog(context, "Error", "Invalid S/N"); } finally { setState(() { _isLoading = false;