-
-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set error state manually? #123
Comments
HI @wsakka, |
Hi @danvick, thanks for your update. I might have misstated the question -- how do I set the errorText for a FormBuilderTextField for instance? |
So there's 2 mechanisms that would be of interest, one is to pass an error state for individual fields, such as:
or passing a map of values: |
This future is very useful, any updates? |
For now, there is an alternative way to do this, hope this could help someone. use custom validate method :
when errors from backend api put errors to a ChangeNotifier.
then just listening changes and validate form again.
|
LGTM! Thanks @danvick 😁 |
If you want to show below the TextField, there’s an InputDecoration attribute called errorText. You just need to get your message and set there. |
Hello I am having a problem for asynchronous control. In my view I have a login form with two inputs based on the same error variable : FormBuilder(
key: _con.formKey,
child: Column(
children: [
CInput(
errorText: _con.inputError,
name: _con.inputEmail,
type: TextInputType.emailAddress,
hint: "Ex : ...",
label: "E-mail",
isRequired: true,
),
CInput(
errorText: _con.inputError,
name: _con.inputPassword,
type: TextInputType.text,
label: "Mot de passe",
isRequired: true,
isObscure: true,
),
],
),
), I have a submit button to reset the value of the error input on each click : CButton(
backgroundColor: _con.hasErrorApi ? CTheme.cWhite : CTheme.cGreen,
borderColor: _con.hasErrorApi ? CTheme.cOrange : CTheme.cGreen,
content: CText(
val: "Se connecter",
color: _con.hasErrorApi ? CTheme.cOrange : CTheme.cWhite,
size: 19,
),
onPressed: () async {
FocusScope.of(context).requestFocus(FocusNode());
setState(() {
_con.changeStateLoader();
_con.removeError();
});
await submitForm();
setState(() {
_con.changeStateLoader();
});
if (_con.user != null) {
Navigator.of(context).pushReplacementNamed('/home');
}
},
) More precisely the removeError function: removeError() {
_inputError = null;
} More precisely the submitForm function: Future<void> submitForm() async {
print(_formKey.currentState.validate());
_formKey.currentState.save();
if (_formKey.currentState.validate()) {
Map<String, dynamic> formValues = _formKey.currentState.value;
String email = formValues[inputEmail];
String password = formValues[inputPassword];
Response apiResponse = await api().authentication(email, password);
Map apiResponseBody = json.decode(apiResponse.body);
if (apiResponse.statusCode != 200) {
this._hasErrorApi = true;
this._messageError = apiResponseBody['message'];
this._inputError = '';
} else {
if (Config.userCanLogin(apiResponseBody['data']['roles'])) {
List<String> roles = apiResponseBody['data']['roles'];
if (roles.contains(Config.roleOwner)) {
print(Owner.fromJsonMin(apiResponseBody['data']).toString());
globals.currentUser = Owner.fromJsonMin(apiResponseBody['data']);
}
}
this._hasErrorApi = true;
this._messageError = "Vous ne disposez pas du rôle adéquat pour utiliser l'application";
this._inputError = '';
}
}
} But when i set inputError to null the function _formKey.currentState.validate() return false... have you ever met cares ? |
In the doc : https://pub.dev/packages/flutter_form_builder/versions/4.0.0-pre.6 i try this : RaisedButton(
child: Text('Submit'),
onPressed: () async {
setState(() => _emailError = null);
if(checkIfEmailExists()){
setState(() => _emailError = 'Email already taken.');
}
},
), but if we print : formKey.currentState.saveAndValidate() it's not sync, when _emailError == null : formKey.currentState.saveAndValidate() == false |
You may have to await the response of
You may have to await the response from RaisedButton(
child: Text('Submit'),
onPressed: () async {
setState(() => _emailError = null);
if( await checkIfEmailExists()){
setState(() => _emailError = 'Email already taken.');
}
if(formKey.currentState.saveAndValidate()){
// Do stuff
}
},
), |
Greetings,
Was building my own builder library but saw this one so much better! Anyway, assuming I'm using a form, and I get a response from the server (This email address is already in use) for instance, how to display that error message?
Thanks,
The text was updated successfully, but these errors were encountered: