Skip to content

Commit

Permalink
Allow null values for decimal fields (#539)
Browse files Browse the repository at this point in the history
* Allow null values for decimal fields

- fixes #538

* Update release notes

* Fix initial value
  • Loading branch information
SchrodingersGat committed Sep 27, 2024
1 parent ad48e5e commit 538a3d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions assets/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 0.16.5 - September 2024
---

- Allow blank values to be entered into numerical fields

### 0.16.4 - September 2024
---

Expand Down
17 changes: 15 additions & 2 deletions lib/api_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,14 @@ class APIFormField {
// Construct a floating point numerical input field
Widget _constructFloatField() {

double initial = double.tryParse(value.toString()) ?? 0;
// Initial value: try to cast to a valid number
String initial = "";

double? initialNumber = double.tryParse(value.toString());

if (initialNumber != null) {
initial = simpleNumberString(initialNumber);
}

return TextFormField(
decoration: InputDecoration(
Expand All @@ -481,9 +488,15 @@ class APIFormField {
helperStyle: _helperStyle(),
hintText: placeholderText,
),
initialValue: simpleNumberString(initial),
initialValue: initial,
keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true),
validator: (value) {
value = value?.trim() ?? "";

// Allow empty numbers, *if* this field is not required
if (value.isEmpty && !required) {
return null;
}

double? quantity = double.tryParse(value.toString());

Expand Down

0 comments on commit 538a3d6

Please sign in to comment.