add validation in input form field

This commit is contained in:
sma
2024-02-28 17:07:23 +06:30
parent c06da8a60c
commit e00545a3d7
8 changed files with 278 additions and 177 deletions

View File

@@ -26,6 +26,7 @@ class _CustomEditorState extends State<CustomEditor> {
bool _isLoading = false;
CargoType _custom = new CargoType();
bool _isNew = false;
final _customFormKey = GlobalKey<FormState>();
@override
void initState() {
@@ -50,16 +51,39 @@ class _CustomEditorState extends State<CustomEditor> {
final productBox = InputText(
labelTextKey: 'rate.cutom.product_type',
iconData: FontAwesomeIcons.weightHanging,
controller: _productController);
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: _productController,
validator: (value){
if(value==null || value.isEmpty){
return "Please insert product type";
}
return null;
},);
final feeBox = InputText(
labelTextKey: 'rate.custom.fee',
iconData: Icons.attach_money,
controller: _feeController);
labelTextKey: 'rate.custom.fee',
iconData: Icons.attach_money,
controller: _feeController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert fee";
}
return null;
},
);
final shipmentRateBox = InputText(
labelTextKey: 'rate.custom.shipment_rate',
iconData: Icons.attach_money,
controller: _shipmentRateController);
labelTextKey: 'rate.custom.shipment_rate',
iconData: Icons.attach_money,
controller: _shipmentRateController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert shipment rate";
}
return null;
},
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
@@ -86,24 +110,27 @@ class _CustomEditorState extends State<CustomEditor> {
)
],
),
body: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
productBox,
feeBox,
shipmentRateBox,
SizedBox(height: 30),
],
body: Form(
key: _customFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
productBox,
feeBox,
shipmentRateBox,
SizedBox(height: 30),
],
),
),
),
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
SizedBox(height: 10)
],
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
SizedBox(height: 10)
],
),
),
),
),
@@ -111,13 +138,7 @@ class _CustomEditorState extends State<CustomEditor> {
}
_save() async {
if (_feeController.text == "") {
showMsgDialog(context, "Error", "Please insert fee");
return;
}
if (_shipmentRateController.text == "") {
showMsgDialog(context, "Error", "Please insert shipment rate");
if (!_customFormKey.currentState!.validate()) {
return;
}