2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00

Set error messgaes for form fields

This commit is contained in:
Oliver 2021-07-16 17:30:31 +10:00
parent e470fb60a2
commit faf20030e3
2 changed files with 68 additions and 26 deletions

View File

@ -34,7 +34,7 @@ class APIResponse {
dynamic data; dynamic data;
// Request is "valid" if a statusCode was returned // Request is "valid" if a statusCode was returned
bool isValid() => statusCode >= 0; bool isValid() => (statusCode >= 0) && (statusCode < 500);
} }
@ -431,7 +431,7 @@ class InvenTreeAPI {
// Perform a PATCH request // Perform a PATCH request
Future<APIResponse> patch(String url, {Map<String, String> body = const {}, int expectedStatusCode=200}) async { Future<APIResponse> patch(String url, {Map<String, String> body = const {}, int? expectedStatusCode}) async {
var _body = Map<String, String>(); var _body = Map<String, String>();
// Copy across provided data // Copy across provided data

View File

@ -35,8 +35,20 @@ class APIFormField {
// Get the "default" as a string // Get the "default" as a string
dynamic get defaultValue => data['default']; dynamic get defaultValue => data['default'];
bool hasErrors() => errorMessages().length > 0;
// Return the error message associated with this field // Return the error message associated with this field
String get errorMessage => data['error']; List<String> errorMessages() {
List<dynamic> errors = data['errors'] ?? [];
List<String> messages = [];
for (dynamic error in errors) {
messages.add(error.toString());
}
return messages;
}
// Is this field required? // Is this field required?
bool get required => (data['required'] ?? false) as bool; bool get required => (data['required'] ?? false) as bool;
@ -98,17 +110,17 @@ class APIFormField {
/* /*
* Extract field options from a returned OPTIONS request * Extract field options from a returned OPTIONS request
*/ */
Map<String, dynamic> extractFields(dynamic options) { Map<String, dynamic> extractFields(APIResponse response) {
if (options == null) { if (!response.isValid()) {
return {}; return {};
} }
if (!options.containsKey("actions")) { if (!response.data.containsKey("actions")) {
return {}; return {};
} }
var actions = options["actions"]; var actions = response.data["actions"];
return actions["POST"] ?? actions["PUT"] ?? actions["PATCH"] ?? {}; return actions["POST"] ?? actions["PUT"] ?? actions["PATCH"] ?? {};
} }
@ -127,10 +139,12 @@ Map<String, dynamic> extractFields(dynamic options) {
Future<void> launchApiForm(String title, String url, Map<String, dynamic> fields, {Map<String, dynamic> modelData = const {}, String method = "PATCH"}) async { Future<void> launchApiForm(String title, String url, Map<String, dynamic> fields, {Map<String, dynamic> modelData = const {}, String method = "PATCH"}) async {
dynamic options = await InvenTreeAPI().options(url); var options = await InvenTreeAPI().options(url);
// null response from server final _formKey = new GlobalKey<FormState>();
if (options == null) {
// Invalid response from server
if (!options.isValid()) {
return; return;
} }
@ -187,7 +201,49 @@ Future<void> launchApiForm(String title, String url, Map<String, dynamic> fields
} }
} }
final _formKey = new GlobalKey<FormState>(); void sendRequest(BuildContext context) async {
// Package up the form data
Map<String, String> formData = {};
for (var field in formFields) {
formData[field.name] = field.value.toString();
}
var response = await InvenTreeAPI().patch(
url,
body: formData,
);
if (!response.isValid()) {
// TODO - Display an error message here...
return;
}
switch (response.statusCode) {
case 200:
case 201:
// Form was validated by the server
Navigator.pop(context);
break;
case 400:
// Update field errors
for (var field in formFields) {
field.data['errors'] = response.data[field.name];
if (field.hasErrors()) {
print("Field '${field.name}' has errors:");
for (String error in field.errorMessages()) {
print(" - ${error}");
}
}
}
break;
}
}
OneContext().showDialog( OneContext().showDialog(
builder: (BuildContext context) { builder: (BuildContext context) {
@ -209,21 +265,7 @@ Future<void> launchApiForm(String title, String url, Map<String, dynamic> fields
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();
// Package up the form data sendRequest(context);
Map<String, String> formData = {};
for (var field in formFields) {
formData[field.name] = field.value.toString();
}
print(formData.toString());
// Send the data to the server
// Respond to error message
// Dismiss the form
} }
}, },
) )