Repository navigation
TextField doesn't respect maxLength limit #67898
Copy link
Copy link
Closed
Labels
P1High-priority issues at the top of the work listHigh-priority issues at the top of the work lista: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsa: typographyText rendering, possibly libtxtText rendering, possibly libtxtc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterc: regressionIt was better in the past than it is nowIt was better in the past than it is nowcustomer: crowdAffects or could affect many people, though not necessarily a specific customer.Affects or could affect many people, though not necessarily a specific customer.found in release: 1.22Found to occur in 1.22Found to occur in 1.22found in release: 1.23Found to occur in 1.23Found to occur in 1.23frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packages
Description
Doctor summary
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.1, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.50.0)
[✓] Connected device (1 available)
Steps to Reproduce
- Create
TextFieldas follows:
TextField(
maxLength: 5,
),
or
TextField(
maxLength: 5,
inputFormatters: [LengthLimitingTextInputFormatter(5)],
),
- Start typing some text
Expected results:
- TextField allows to enter only
maxLengthcharacters
Actual results:
- it's possible to enter more than
maxLengthcharacters
Looks like it's related to the following change introduced in flutter 1.22 - https://github.com/flutter/flutter/pull/63754/files
packages/flutter/lib/src/services/text_formatter.dart
///
/// ### Composing text behaviors
///
/// There is no guarantee for the final value before the composing ends.
/// So while the value is composing, the constraint of [maxLength] will be
/// temporary lifted until the composing ends.
///
/// In addition, if the current value already reached the [maxLength],
/// composing is not allowed.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P1High-priority issues at the top of the work listHigh-priority issues at the top of the work lista: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsa: typographyText rendering, possibly libtxtText rendering, possibly libtxtc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterc: regressionIt was better in the past than it is nowIt was better in the past than it is nowcustomer: crowdAffects or could affect many people, though not necessarily a specific customer.Affects or could affect many people, though not necessarily a specific customer.found in release: 1.22Found to occur in 1.22Found to occur in 1.22found in release: 1.23Found to occur in 1.23Found to occur in 1.23frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packages

Activity
Gridzzle commentedon Oct 12, 2020
try :
maxLengthEnforced: true,
instead of inputFormatters
gocal commentedon Oct 12, 2020
@Gridzzle maxLengthEnforced is by default set to
trueso it doesn't change anything.Gridzzle commentedon Oct 12, 2020
it works perfect with me
xu-baolin commentedon Oct 12, 2020
@gocal Hi
This is designed.
You are during Composing time, see also
flutter/packages/flutter/lib/src/services/text_formatter.dart
Line 345 in 2c3652b
gocal commentedon Oct 12, 2020
@xu-baolin I've noticed this behavior has been introduced in 1.22 release (https://github.com/flutter/flutter/pull/63754/files) but I think this is not how I'd expect "maxLength" would work.
darshankawar commentedon Oct 12, 2020
@gocal
There are similar open issues describing your case, 67236 and 67828 which also talks about a related deleting characters in textfield.
I'd suggest you to follow-up in above issues for updates and any questions.
Closing this as duplicate. If you disagree, write in comments and I'll reopen.
Thanks.
xu-baolin commentedon Oct 12, 2020
Not the same issue herehere :)
pedromassangocode commentedon Oct 12, 2020
I was able to reproduce this issue on latest
masterandstablechannel. Testing the same code withFlutter 1.20.0we have a different behavior (the correct one). I also believemaxLengthshould be respected all the time.This was introduced by #63754.
Cc @justinmc
sample code
flutter doctor
83 remaining items
Faiyyazauxy commentedon Dec 4, 2020
Even i am seeing maxlength problem now previously it used to work fine. Even inputformatters are not working for length. Please help.
And yes the number first maxlength behaviour works awesome like what others has mentioned
Faiyyazauxy commentedon Dec 4, 2020
Awesome this works fine as it's suppose to
AlexV525 commentedon Dec 4, 2020
@Faiyyazauxy There is a valid workaround at here: #67898 (comment)
Faiyyazauxy commentedon Dec 4, 2020
Okay @AlexV525 Thanks will try and check
Faiyyazauxy commentedon Dec 4, 2020
This is the flutter's default behaviour i need to stop the users typing beyond the maxlength. This lets the user type even after the maxlength and then remove the text after the maxlength on submit. Anyway thanks for the help
AlexV525 commentedon Dec 6, 2020
@derencewalk1 From your screenshots, number at first usually means that you're not typing a word, so the keyboard didn't bring you into composition, which will be enforced truncate because it has no composition. There are no special handle compare to typing a regular word.
AlexV525 commentedon Dec 9, 2020
Hi all! The PR has landed on the master with some behaviors update. Basically we make the truncate behavior follow the platform that users on by default. If anyone still encountering some similar issues, feel free to provide more info or use cases, and it's better to file a new issue. Thanks for all your patience for participating our discussions and waiting this to land.
RalphNoel commentedon Dec 14, 2020
@AlexV525 Hi, it's working fine in when i set KeyboardType is number, but maxLength is not working in AlphaNumeric Characters.
For Eg.
I tried to set a bank IFS Code(INDIA) in TextFormField, the maxLength is not working in this Condition..
cassioseffrin commentedon Dec 16, 2020
In my opinion this issue was closed without a good argument. The version above the 1.20 have this breaking change, the argument that Gboard is creating a composing region isn't enough.
If anyone have found a solution to limit the text input by length it will be very appreciated by the flutter community.
AlexV525 commentedon Dec 16, 2020
Comments that follow the code of conduct are always welcome, but IMO after 96+213 comments and 87 commits we do have the most effective solution. You can provide more info about your proposal in a new issue in anytime.
cassioseffrin commentedon Dec 17, 2020
Today I have made a test on the dev channel and the maxLength is working in both keyboard types (numeric/alpha numeric). I will appreciate to know when those commits will be in the stable channel.
Okladnoj commentedon May 18, 2021
github-actions commentedon Aug 7, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.