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

@@ -19,6 +19,7 @@ class _InvitationCreateState extends State<InvitationCreate> {
bool _isLoading = false;
late String dialCode;
final _inviteFormKey = GlobalKey<FormState>();
@override
void initState() {
@@ -36,7 +37,15 @@ class _InvitationCreateState extends State<InvitationCreate> {
final nameBox = InputText(
labelTextKey: 'customer.name',
iconData: Icons.person,
controller: _nameController);
controller: _nameController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert name";
}
return null;
},
);
return LocalProgress(
inAsyncCall: _isLoading,
@@ -56,7 +65,9 @@ class _InvitationCreateState extends State<InvitationCreate> {
}
},
),
body: Container(
body: Form(
key: _inviteFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: ListView(
children: <Widget>[
@@ -75,7 +86,8 @@ class _InvitationCreateState extends State<InvitationCreate> {
decoration: BoxDecoration(
border:
Border.all(color: Colors.grey.shade400, width: 1),
borderRadius: BorderRadius.all(Radius.circular(12.0))),
borderRadius:
BorderRadius.all(Radius.circular(12.0))),
child: CountryCodePicker(
onChanged: _countryChange,
initialSelection: dialCode,
@@ -83,11 +95,12 @@ class _InvitationCreateState extends State<InvitationCreate> {
showCountryOnly: false,
showOnlyCountryWhenClosed: false,
alignLeft: false,
textStyle: TextStyle(fontSize: 16, color: Colors.black87),
textStyle:
TextStyle(fontSize: 16, color: Colors.black87),
searchDecoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.black, width: 1.0))),
borderSide: BorderSide(
color: Colors.black, width: 1.0))),
),
),
SizedBox(
@@ -106,14 +119,24 @@ class _InvitationCreateState extends State<InvitationCreate> {
),
decoration: InputDecoration(
fillColor: Colors.white,
labelText: getLocalString(context, "customer.phone"),
labelText:
getLocalString(context, "customer.phone"),
labelStyle:
TextStyle(fontSize: 16, color: Colors.grey),
filled: true,
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.grey, width: 1.0)),
),
borderSide: BorderSide(
color: Colors.grey, width: 1.0)),
errorStyle: TextStyle(
color: dangerColor,
)),
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert phone no";
}
return null;
},
autovalidateMode: AutovalidateMode.onUserInteraction,
),
),
),
@@ -126,6 +149,7 @@ class _InvitationCreateState extends State<InvitationCreate> {
),
),
),
),
);
}
@@ -138,9 +162,9 @@ class _InvitationCreateState extends State<InvitationCreate> {
_invite() async {
String userName = _nameController.text;
String phoneNumber = dialCode + _phoneController.text;
if (userName == "" || phoneNumber == "") {
showMsgDialog(context, "Error", "Invalid name or phone number");
return;
if (!_inviteFormKey.currentState!.validate()) {
return null;
}
setState(() {
_isLoading = true;

View File

@@ -121,6 +121,7 @@ class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
labelTextKey: "FCSshipment.number",
iconData: Ionicons.ios_airplane,
controller: _shipmentNumberController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value!.isEmpty) {
return "Enter shipment number";
@@ -132,6 +133,7 @@ class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
labelTextKey: "FCSshipment.cutoff_date",
iconData: Icons.date_range,
controller: _cutoffDateController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value!.isEmpty) {
return "Select cutoff date";
@@ -143,6 +145,7 @@ class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
labelTextKey: "FCSshipment.ETA",
iconData: Icons.date_range,
controller: _arrivalDateController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value!.isEmpty) {
return "Select ETA date";

View File

@@ -24,6 +24,7 @@ class _CargoEditorState extends State<CargoEditor> {
bool _isLoading = false;
late CargoType _cargo;
bool _isNew = false;
final _cargoFormKey = GlobalKey<FormState>();
@override
void initState() {
@@ -47,11 +48,26 @@ class _CargoEditorState extends State<CargoEditor> {
final typeBox = InputText(
labelTextKey: 'cargo.type',
iconData: Icons.text_format,
controller: _descController);
controller: _descController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value){
if(value==null || value.isEmpty){
return "Please insert cargo type";
}
return null;
},);
final rateBox = InputText(
labelTextKey: 'cargo.rate',
iconData: Icons.attach_money,
controller: _rateController);
controller: _rateController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert rate";
}
return null;
},
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
@@ -78,7 +94,9 @@ class _CargoEditorState extends State<CargoEditor> {
)
],
),
body: Container(
body: Form(
key: _cargoFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
@@ -98,12 +116,12 @@ class _CargoEditorState extends State<CargoEditor> {
),
),
),
),
);
}
_save() async {
if (_rateController.text == "") {
showMsgDialog(context, "Error", "Please insert rate");
if (!_cargoFormKey.currentState!.validate()) {
return;
}
setState(() {

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);
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);
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,7 +110,9 @@ class _CustomEditorState extends State<CustomEditor> {
)
],
),
body: Container(
body: Form(
key: _customFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
@@ -107,17 +133,12 @@ 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;
}

View File

@@ -24,6 +24,7 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
bool _isLoading = false;
bool _isNew = false;
DiscountByWeight _discountByWeight = new DiscountByWeight();
final _discountFormKey = GlobalKey<FormState>();
@override
void initState() {
@@ -48,11 +49,27 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
final weightBox = InputText(
labelTextKey: 'rate.discount.weight',
iconData: FontAwesomeIcons.weightHanging,
controller: _weightController);
controller: _weightController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please inset weight";
}
return null;
},
);
final discountRateBox = InputText(
labelTextKey: 'rate.discount.rate',
iconData: Icons.attach_money,
controller: _discountController);
controller: _discountController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert discount rate";
}
return null;
},
);
return LocalProgress(
inAsyncCall: _isLoading,
@@ -80,7 +97,9 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
)
],
),
body: Container(
body: Form(
key: _discountFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
@@ -100,17 +119,12 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
),
),
),
),
);
}
_save() async {
if (_weightController.text == "") {
showMsgDialog(context, "Error", "Please insert weight");
return;
}
if (_discountController.text == "") {
showMsgDialog(context, "Error", "Please insert discount rate");
if (!_discountFormKey.currentState!.validate()) {
return;
}

View File

@@ -30,6 +30,7 @@ class _ReceivingEditorState extends State<ReceivingEditor> {
bool _isLoading = false;
late bool _isNew;
User? user;
final _receiveFormKey=GlobalKey<FormState>();
TextEditingController _trackingIDCtl = new TextEditingController();
TextEditingController _remarkCtl = new TextEditingController();
MultiImgController _multiImgController = MultiImgController();
@@ -93,6 +94,14 @@ class _ReceivingEditorState extends State<ReceivingEditor> {
iconData: MaterialCommunityIcons.barcode_scan,
labelTextKey: "receiving.tracking.id",
controller: _trackingIDCtl,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value){
if(value==null || value.isEmpty){
return "invalid tracking ID";
}
return null;
},
)),
InkWell(
onTap: _scan,
@@ -159,6 +168,8 @@ class _ReceivingEditorState extends State<ReceivingEditor> {
),
body: Padding(
padding: const EdgeInsets.only(left: 12.0, right: 12),
child: Form(
key: _receiveFormKey,
child: ListView(
children: <Widget>[
trackingIDBox,
@@ -184,6 +195,7 @@ class _ReceivingEditorState extends State<ReceivingEditor> {
],
),
),
),
));
}
@@ -216,8 +228,11 @@ class _ReceivingEditorState extends State<ReceivingEditor> {
Package _p =
Package(trackingID: _trackingIDCtl.text, remark: _remarkCtl.text);
if (_p.trackingID == null || _p.trackingID == "") {
showMsgDialog(context, "Error", "Invalid tracking ID!");
// if (_p.trackingID == null || _p.trackingID == "") {
// showMsgDialog(context, "Error", "Invalid tracking ID!");
// return;
// }
if (!_receiveFormKey.currentState!.validate()) {
return;
}

View File

@@ -16,6 +16,7 @@ class InputDate extends StatelessWidget {
final TextInputType? textInputType;
final bool autoFocus;
final String dateFormatString;
final AutovalidateMode? autovalidateMode;
const InputDate(
{Key? key,
@@ -23,6 +24,7 @@ class InputDate extends StatelessWidget {
required this.iconData,
required this.controller,
this.validator,
this.autovalidateMode,
this.maxLines = 1,
this.withBorder = false,
this.borderColor,
@@ -126,7 +128,8 @@ class InputDate extends StatelessWidget {
borderSide: BorderSide(
color: borderColor ?? dangerColor, width: 1.0)),
),
validator: validator),
validator: validator,
autovalidateMode: autovalidateMode,),
);
}
}

View File

@@ -9,6 +9,7 @@ class InputText extends StatelessWidget {
final IconData? iconData;
final TextEditingController? controller;
final FormFieldValidator<String>? validator;
final AutovalidateMode? autovalidateMode;
final int maxLines;
final bool withBorder;
final Color? borderColor;
@@ -26,6 +27,7 @@ class InputText extends StatelessWidget {
this.iconData,
this.controller,
this.validator,
this.autovalidateMode,
this.maxLines = 1,
this.withBorder = false,
this.borderColor,
@@ -114,7 +116,8 @@ class InputText extends StatelessWidget {
borderSide: BorderSide(
color: borderColor ?? dangerColor, width: 1.0)),
),
validator: validator),
validator: validator,
autovalidateMode: autovalidateMode,),
);
}
}