From ca6226a596327f0bffea684f79ae05e0a00737ba Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 20 Jul 2021 19:14:44 +1000 Subject: [PATCH] Form updates --- lib/api_form.dart | 116 +++++++++++++++++++++++------------- lib/l10n | 2 +- lib/widget/part_detail.dart | 12 ++-- 3 files changed, 81 insertions(+), 49 deletions(-) diff --git a/lib/api_form.dart b/lib/api_form.dart index 24b4b351..e0cf4578 100644 --- a/lib/api_form.dart +++ b/lib/api_form.dart @@ -63,6 +63,7 @@ class APIFormField { Widget constructField() { switch (type) { case "string": + case "url": return _constructString(); case "boolean": return _constructBoolean(); @@ -86,8 +87,9 @@ class APIFormField { data["value"] = val; }, validator: (value) { - - // TODO - Custom field validation + if (required && (value == null || value.isEmpty)) { + return L10().valueCannotBeEmpty; + } }, ); } @@ -193,14 +195,39 @@ Future launchApiForm(String title, String url, Map fields formFields.add(APIFormField(fieldName, remoteField)); } - List widgets = []; + List buildWidgets() { + List widgets = []; + + for (var ff in formFields) { + + if (ff.hidden) { + continue; + } - for (var ff in formFields) { - if (!ff.hidden) { widgets.add(ff.constructField()); + + if (ff.hasErrors()) { + for (String error in ff.errorMessages()) { + widgets.add( + ListTile( + title: Text( + error, + style: TextStyle(color: Color.fromRGBO(250, 50, 50, 1)) + ), + ) + ); + } + } + } + + return widgets; } + + List _widgets = buildWidgets(); + + void sendRequest(BuildContext context) async { // Package up the form data @@ -252,44 +279,51 @@ Future launchApiForm(String title, String url, Map fields OneContext().showDialog( builder: (BuildContext context) { - return AlertDialog( - title: Text(title), - actions: [ - // Cancel button - TextButton( - child: Text(L10().cancel), - onPressed: () { - Navigator.pop(context); + return StatefulBuilder( + builder: (context, setState) { + return AlertDialog( + title: Text(title), + actions: [ + // Cancel button + TextButton( + child: Text(L10().cancel), + onPressed: () { + Navigator.pop(context); - if (onCancel != null) { - onCancel(); - } - }, - ), - // Save button - TextButton( - child: Text(L10().save), - onPressed: () { - // Validate the form - if (_formKey.currentState!.validate()) { - _formKey.currentState!.save(); + if (onCancel != null) { + onCancel(); + } + }, + ), + // Save button + TextButton( + child: Text(L10().save), + onPressed: () { + // Validate the form + if (_formKey.currentState!.validate()) { + _formKey.currentState!.save(); - sendRequest(context); - } - }, - ) - ], - content: Form( - key: _formKey, - child: SingleChildScrollView( - child: Column( - mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: widgets - ) - ) - ) + setState(() { + sendRequest(context); + _widgets = buildWidgets(); + }); + } + }, + ) + ], + content: Form( + key: _formKey, + child: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: _widgets, + ) + ) + ) + ); + } ); } ); diff --git a/lib/l10n b/lib/l10n index 84f6ed3f..0870256f 160000 --- a/lib/l10n +++ b/lib/l10n @@ -1 +1 @@ -Subproject commit 84f6ed3faf63cbf371016e134196400e5f822759 +Subproject commit 0870256fb97a27ccf0ab73b4665b886ca0abb4aa diff --git a/lib/widget/part_detail.dart b/lib/widget/part_detail.dart index 2f058d9a..bb01ea0e 100644 --- a/lib/widget/part_detail.dart +++ b/lib/widget/part_detail.dart @@ -179,21 +179,19 @@ class _PartDisplayState extends RefreshableState { var _keywords; var _link; - launchApiForm( - "Edit Part", + L10().editPart, part.url, { "name": {}, "description": {}, - "IPN": { - "hidden": true, - "label": "My custom label!", - }, + "IPN": {}, + "keywords": {}, "active": {}, + "link": {}, }, modelData: part.jsondata, - onSuccess: refresh, + onSuccess: refresh, ); return;