Improve form validation

This commit is contained in:
Hillel Coren 2022-10-02 12:16:45 +03:00
parent c77964b7f1
commit de91df3af9
18 changed files with 4 additions and 107 deletions

View File

@ -15,7 +15,6 @@ class DecoratedFormField extends StatefulWidget {
this.controller,
this.label,
this.onSavePressed,
this.autovalidate = false,
this.autocorrect = false,
this.obscureText = false,
this.onChanged,
@ -48,7 +47,6 @@ class DecoratedFormField extends StatefulWidget {
final TextInputType keyboardType;
final int maxLines;
final int minLines;
final bool autovalidate;
final bool enabled;
final bool autocorrect;
final bool obscureText;
@ -140,13 +138,11 @@ class _DecoratedFormFieldState extends State<DecoratedFormField> {
autofocus: widget.autofocus,
decoration: inputDecoration,
validator: widget.validator,
autovalidateMode: AutovalidateMode.onUserInteraction,
keyboardType: widget.keyboardType,
maxLines: widget.expands ? null : widget.maxLines ?? 1,
minLines: widget.expands ? null : widget.minLines,
expands: widget.expands,
autovalidateMode: widget.autovalidate
? AutovalidateMode.onUserInteraction
: AutovalidateMode.disabled,
autocorrect:
widget.isMoney || widget.isPercent ? false : widget.autocorrect,
obscureText: widget.obscureText,

View File

@ -8,7 +8,6 @@ import 'package:invoiceninja_flutter/utils/localization.dart';
class PasswordFormField extends StatefulWidget {
const PasswordFormField({
this.controller,
this.autoValidate,
this.newPassword = true,
this.validate = true,
this.onSavePressed,
@ -17,7 +16,6 @@ class PasswordFormField extends StatefulWidget {
final TextEditingController controller;
final Function(BuildContext) onSavePressed;
final bool autoValidate;
final bool newPassword;
final bool validate;
final String labelText;
@ -44,7 +42,6 @@ class _PasswordFormFieldState extends State<PasswordFormField> {
controller: widget.controller,
onSavePressed: widget.onSavePressed,
autocorrect: false,
autovalidate: widget.autoValidate,
decoration: InputDecoration(
labelText: widget.labelText ?? localization.password,
suffixIcon: IconButton(

View File

@ -77,7 +77,6 @@ class _LoginState extends State<LoginView> {
bool _createAccount = false;
bool _recoverPassword = false;
bool _autoValidate = false;
bool _termsChecked = false;
bool _privacyChecked = false;
@ -157,7 +156,6 @@ class _LoginState extends State<LoginView> {
: kAppProductionUrl;
setState(() {
_autoValidate = !isValid ?? false;
_loginError = '';
});
@ -223,7 +221,6 @@ class _LoginState extends State<LoginView> {
final viewModel = widget.viewModel;
setState(() {
_autoValidate = !isValid ?? false;
_loginError = '';
});
@ -428,7 +425,6 @@ class _LoginState extends State<LoginView> {
controller: _emailController,
label: localization.email,
keyboardType: TextInputType.emailAddress,
autovalidate: _autoValidate,
validator: (val) =>
val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterYourEmail
@ -441,7 +437,6 @@ class _LoginState extends State<LoginView> {
!_recoverPassword)
PasswordFormField(
controller: _passwordController,
autoValidate: false,
newPassword: _createAccount,
onSavePressed: (_) => _submitForm(),
),
@ -470,7 +465,6 @@ class _LoginState extends State<LoginView> {
labelText:
'${localization.secret} (${localization.optional})',
controller: _secretController,
autoValidate: _autoValidate,
validate: false,
onSavePressed: (_) => _submitForm(),
),

View File

@ -27,7 +27,6 @@ class _BankAccountEditState extends State<BankAccountEdit> {
static final GlobalKey<FormState> _formKey =
GlobalKey<FormState>(debugLabel: '_bankAccountEdit');
final FocusScopeNode _focusNode = FocusScopeNode();
bool _autoValidate = false;
final _nameController = TextEditingController();
@ -88,10 +87,6 @@ class _BankAccountEditState extends State<BankAccountEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid) {
return;
}
@ -115,7 +110,6 @@ class _BankAccountEditState extends State<BankAccountEdit> {
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAName
: null,
autovalidate: _autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.text,
),

View File

@ -251,12 +251,6 @@ class _DesignEditState extends State<DesignEdit>
: (context) {
final bool isValid = _formKey.currentState.validate();
/*
setState(() {
_autoValidate = !isValid;
});
*/
if (!isValid) {
return;
}
@ -458,6 +452,8 @@ class _DesignSettingsState extends State<DesignSettings> {
label: localization.name,
controller: widget.nameController,
keyboardType: TextInputType.text,
validator: (value) =>
value.isEmpty ? localization.pleaseEnterAName : null,
),
DesignPicker(
label: localization.design,

View File

@ -27,7 +27,6 @@ class _ExpenseCategoryEditState extends State<ExpenseCategoryEdit> {
static final GlobalKey<FormState> _formKey =
GlobalKey<FormState>(debugLabel: '_expenseCategoryEdit');
final _debouncer = Debouncer();
bool _autoValidate = false;
// STARTER: controllers - do not remove comment
final _nameController = TextEditingController();
@ -86,10 +85,6 @@ class _ExpenseCategoryEditState extends State<ExpenseCategoryEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid) {
return;
}
@ -107,7 +102,6 @@ class _ExpenseCategoryEditState extends State<ExpenseCategoryEdit> {
DecoratedFormField(
autofocus: true,
controller: _nameController,
autovalidate: _autoValidate,
label: localization.name,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.text,

View File

@ -32,7 +32,6 @@ class _GroupEditState extends State<GroupEdit> {
List<TextEditingController> _controllers = [];
final _debouncer = Debouncer();
bool autoValidate = false;
@override
void didChangeDependencies() {
@ -85,10 +84,6 @@ class _GroupEditState extends State<GroupEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
autoValidate = !isValid ?? false;
});
if (!isValid) {
return;
}
@ -112,7 +107,6 @@ class _GroupEditState extends State<GroupEdit> {
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAValue
: null,
autovalidate: autoValidate,
),
],
),

View File

@ -32,7 +32,6 @@ class _ProductEditState extends State<ProductEdit> {
static final GlobalKey<FormState> _formKey =
GlobalKey<FormState>(debugLabel: '_productEdit');
final FocusScopeNode _focusNode = FocusScopeNode();
bool _autoValidate = false;
final _productKeyController = TextEditingController();
final _notesController = TextEditingController();
@ -149,10 +148,6 @@ class _ProductEditState extends State<ProductEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid) {
return;
}
@ -175,7 +170,6 @@ class _ProductEditState extends State<ProductEdit> {
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAProductKey
: null,
autovalidate: _autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.text,
),

View File

@ -35,8 +35,6 @@ class _ProjectEditState extends State<ProjectEdit> {
GlobalKey<FormState>(debugLabel: '_projectEdit');
final _debouncer = Debouncer();
bool _autoValidate = false;
final _numberController = TextEditingController();
final _nameController = TextEditingController();
final _dueDateController = TextEditingController();
@ -132,10 +130,6 @@ class _ProjectEditState extends State<ProjectEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid) {
return;
}
@ -156,7 +150,6 @@ class _ProjectEditState extends State<ProjectEdit> {
validator: (String val) => val.trim().isEmpty
? localization.pleaseEnterAName
: null,
autovalidate: _autoValidate,
keyboardType: TextInputType.text,
autofocus: true,
label: localization.projectName,
@ -175,7 +168,6 @@ class _ProjectEditState extends State<ProjectEdit> {
validator: (String val) => val.trim().isEmpty
? localization.pleaseSelectAClient
: null,
autoValidate: _autoValidate,
onSelected: (client) {
viewModel.onChanged(project.rebuild(
(b) => b..clientId = client?.id ?? ''));

View File

@ -53,7 +53,6 @@ class _ClientPortalState extends State<ClientPortal>
TabController _controller;
final _webClient = WebClient();
bool _autoValidate = false;
bool _isSubdomainUnique = true;
bool _isCheckingSubdomain = false;
@ -218,10 +217,6 @@ class _ClientPortalState extends State<ClientPortal>
void _onSavePressed(BuildContext context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid || _isCheckingSubdomain) {
return;
}
@ -304,7 +299,6 @@ class _ClientPortalState extends State<ClientPortal>
company.portalMode == kClientPortalModeSubdomain) ...[
DecoratedFormField(
label: localization.subdomain,
autovalidate: _autoValidate,
controller: _subdomainController,
keyboardType: TextInputType.text,
hint: localization.subdomainHelp,

View File

@ -53,7 +53,6 @@ class _CompanyDetailsState extends State<CompanyDetails>
final FocusScopeNode _focusNode = FocusScopeNode();
TabController _controller;
bool autoValidate = false;
final _debouncer = Debouncer(sendFirstAction: true);
final _nameController = TextEditingController();
@ -272,7 +271,6 @@ class _CompanyDetailsState extends State<CompanyDetails>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAName
: null,
autovalidate: autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.text,
),

View File

@ -48,7 +48,6 @@ class _SettingsWizardState extends State<SettingsWizard> {
final FocusScopeNode _focusNode = FocusScopeNode();
final _debouncer = Debouncer(milliseconds: kMillisecondsToDebounceSave);
bool _autoValidate = false;
bool _isSaving = false;
bool _isSubdomainUnique = false;
bool _isCheckingSubdomain = false;
@ -127,10 +126,6 @@ class _SettingsWizardState extends State<SettingsWizard> {
void _onSavePressed() {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid || _isCheckingSubdomain) {
return;
}
@ -196,7 +191,6 @@ class _SettingsWizardState extends State<SettingsWizard> {
final companyName = DecoratedFormField(
autofocus: true,
label: localization.companyName,
autovalidate: _autoValidate,
controller: _nameController,
validator: (value) =>
value.isEmpty ? localization.pleaseEnterAValue : null,
@ -205,7 +199,6 @@ class _SettingsWizardState extends State<SettingsWizard> {
final firstName = DecoratedFormField(
label: localization.firstName,
autovalidate: _autoValidate,
controller: _firstNameController,
keyboardType: TextInputType.name,
autofillHints: [AutofillHints.givenName],
@ -215,7 +208,6 @@ class _SettingsWizardState extends State<SettingsWizard> {
final lastName = DecoratedFormField(
label: localization.lastName,
autovalidate: _autoValidate,
controller: _lastNameController,
keyboardType: TextInputType.name,
autofillHints: [AutofillHints.familyName],
@ -278,7 +270,6 @@ class _SettingsWizardState extends State<SettingsWizard> {
final subdomain = DecoratedFormField(
label: localization.subdomain,
autovalidate: _autoValidate,
controller: _subdomainController,
keyboardType: TextInputType.text,
validator: (value) {

View File

@ -55,7 +55,6 @@ class _UserDetailsState extends State<UserDetails>
GlobalKey<FormState>(debugLabel: '_userDetails');
final FocusScopeNode _focusNode = FocusScopeNode();
TabController _controller;
bool autoValidate = false;
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
@ -136,10 +135,6 @@ class _UserDetailsState extends State<UserDetails>
void _onSavePressed(BuildContext context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
autoValidate = !isValid ?? false;
});
if (!isValid) {
return;
}
@ -320,7 +315,6 @@ class _UserDetailsState extends State<UserDetails>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAFirstName
: null,
autovalidate: autoValidate,
onSavePressed: _onSavePressed,
keyboardType: TextInputType.name,
),
@ -330,7 +324,6 @@ class _UserDetailsState extends State<UserDetails>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterALastName
: null,
autovalidate: autoValidate,
onSavePressed: _onSavePressed,
keyboardType: TextInputType.name,
),
@ -340,7 +333,6 @@ class _UserDetailsState extends State<UserDetails>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterYourEmail
: null,
autovalidate: autoValidate,
onSavePressed: _onSavePressed,
keyboardType: TextInputType.emailAddress,
),
@ -352,7 +344,6 @@ class _UserDetailsState extends State<UserDetails>
),
PasswordFormField(
controller: _passwordController,
autoValidate: autoValidate,
onSavePressed: _onSavePressed,
),
]),

View File

@ -27,7 +27,6 @@ class _TaskStatusEditState extends State<TaskStatusEdit> {
static final GlobalKey<FormState> _formKey =
GlobalKey<FormState>(debugLabel: '_taskStatusEdit');
final _debouncer = Debouncer();
bool _autoValidate = false;
final _nameController = TextEditingController();
@ -84,10 +83,6 @@ class _TaskStatusEditState extends State<TaskStatusEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid) {
return;
}
@ -104,7 +99,6 @@ class _TaskStatusEditState extends State<TaskStatusEdit> {
DecoratedFormField(
autofocus: true,
controller: _nameController,
autovalidate: _autoValidate,
label: localization.name,
keyboardType: TextInputType.text,
validator: (val) => val.isEmpty || val.trim().isEmpty

View File

@ -28,7 +28,6 @@ class _TaxRateEditState extends State<TaxRateEdit> {
GlobalKey<FormState>(debugLabel: '_taxRateEdit');
FocusScopeNode _focusNode;
bool autoValidate = false;
final _nameController = TextEditingController();
final _rateController = TextEditingController();
@ -101,7 +100,6 @@ class _TaxRateEditState extends State<TaxRateEdit> {
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAName
: null,
autovalidate: autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.text,
),

View File

@ -30,7 +30,6 @@ class _TokenEditState extends State<TokenEdit> {
final _nameController = TextEditingController();
List<TextEditingController> _controllers = [];
bool _autoValidate = false;
@override
void didChangeDependencies() {
@ -81,10 +80,6 @@ class _TokenEditState extends State<TokenEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
_autoValidate = !isValid;
});
if (!isValid) {
return;
}
@ -102,7 +97,6 @@ class _TokenEditState extends State<TokenEdit> {
autofocus: true,
controller: _nameController,
label: localization.name,
autovalidate: _autoValidate,
validator: (value) =>
value.isEmpty || value.trim().isEmpty
? localization.pleaseEnterAName

View File

@ -40,7 +40,6 @@ class _UserEditState extends State<UserEdit>
final _debouncer = Debouncer();
final FocusScopeNode _focusNode = FocusScopeNode();
TabController _controller;
bool autoValidate = false;
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
@ -171,10 +170,6 @@ class _UserEditState extends State<UserEdit>
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
setState(() {
autoValidate = !isValid ?? false;
});
if (!isValid) {
return;
}
@ -197,7 +192,6 @@ class _UserEditState extends State<UserEdit>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterAFirstName
: null,
autovalidate: autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.name,
),
@ -207,7 +201,6 @@ class _UserEditState extends State<UserEdit>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterALastName
: null,
autovalidate: autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.name,
),
@ -217,7 +210,6 @@ class _UserEditState extends State<UserEdit>
validator: (val) => val.isEmpty || val.trim().isEmpty
? localization.pleaseEnterYourEmail
: null,
autovalidate: autoValidate,
onSavePressed: viewModel.onSavePressed,
keyboardType: TextInputType.emailAddress,
),

View File

@ -77,12 +77,6 @@ class _StubEditState extends State<StubEdit> {
onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate();
/*
setState(() {
_autoValidate = !isValid;
});
*/
if (!isValid) {
return;
}