Reputation: 1198
I have a form with validation. Everything works correctly when adding text. When deleting text, everything is also correct. But if I remove the very last character then the validation event doesn't fire. Why might this be happening? I need to validate the form for empty input, but I cannot do this, because when the last character is removed, the validation event does not fire.
class CalcWidget extends StatefulWidget {
const CalcWidget({
Key? key,
required this.nomenclature,
}) : super(key: key);
final Nomenclature nomenclature;
@override
_CalcWidgetState createState() => _CalcWidgetState();
}
class _CalcWidgetState extends State<CalcWidget> {
final _formKey = GlobalKey<FormState>();
final _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.only(top: 15, left: 10, right: 15),
child: Column(
children: [
TextFormField(
onChanged: doMath,
// keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9.]+')),
],
keyboardType:
TextInputType.numberWithOptions(decimal: true),
controller: _controller,
decoration: InputDecoration(
filled: true,
),
validator: (value) {
if (value == null || value.isEmpty) {
setState(() {
_sumValue = "";
_discountValue = "—";
});
return "Empty input";
}
if (num.parse(value) >
num.parse(widget.nomenclature.a011)) {
setState(() {
_sumValue = "";
_discountValue = "—";
});
return "Error quantity";
}
return null;
},
),
Padding(
padding: const EdgeInsets.only(top: 8),
child: ListTile(
title: Text(_priceName),
trailing: Text(_priceValue),
),
),
Padding(
padding: const EdgeInsets.only(top: 8),
child: ListTile(
title: Text("Discount"),
trailing: Text(_discountValue),
),
),
Padding(
padding: const EdgeInsets.only(top: 2),
child: ListTile(
title: Text("Sum"),
trailing: Text(_sumValue),
),
),
],
),
)),
),
);
}
Upvotes: 1
Views: 838
Reputation: 3225
Try adding:
autovalidateMode: AutovalidateMode.always
to your TextFormField
.
(autovalidateMode: AutovalidateMode.onUserInteraction
might work too)
Upvotes: 1