Xamarin.forms: [Enhancement] Entry.MaxLength

Created on 26 Jan 2018  Â·  11Comments  Â·  Source: xamarin/Xamarin.Forms

Rational

Sometimes the user needs to limit the maximum number of characters a user can enter into an Entry. While this could be done with a reactive implementation, sometimes setting a property is easier.

Implementation

public class Entry : View
{
    <SNIP>

    public uint MaxLength { get; set; } // BP, Default MaxValue
}

Do not set the native property unless the user changes the MaxLength away from MaxValue otherwise breakage of existing applications may occur.

Expected Result

When the user attempts to enter more text than the max length it should clip at the limit.

Android

Control.SetFilters(new []{ new InputFilter.LengthFilter(length) });

Make sure to pay attention to the comment from Shane below.

iOS

Basically needs to do this: https://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield

DO NOT COPY AND PASTE CODE.

UWP

Just set the MaxLength property of the TextBox.

Implications for CSS

Not relevant to CSS

Backward Compatibility

Third party renderers may need to be updated to ensure that this functionality is supported through the new official mechanism. Further we will need to be careful to code the changes to the renderers in a careful manner to ensure that if someone is already using an effect support this feature that the effect is as best as possible not broken by our changes.

Difficulty : Easy

The implementation is relatively easy on each target platform. It is unfortunately not possible to detect if the user has already set these values on all platforms. This will make avoid backwards-compatibility issues much harder.

F100 community-sprint enhancement âž•

All 11 comments

For backwards compatibility on Android does it need to merge the LengthFilter into whatever filters are returned from Control.GetFilters() ? This way it doesn't overwrite any other filters that were already applied?

Agreed, thats a really good point.

I can take this!

@alanag13 did you do anything yet? I think I indicated to @davidortinau that I would pick this up :)

@jfversluis this one's all yours!

@jassmith @kingces95 This proposal has MaxLength as a uint which is inconsistent with the CursorPosition type in https://github.com/xamarin/Xamarin.Forms/issues/1667. Any reason that MaxLength shouldn't be an int? That would make it consistent with UWP TextBox max length; EditText on Android appears to use an int as well.

@hartez I would have thought that CursorPosition and SelectionLength in #1667 should be uint on the basis that negative numbers are not valid for position, selection length and max length here. Either that or appropriate range validation should be done?

True, if we use an int, we will have to check it for < 0. That could take place in the BindableProperty.

If we use a uint, we still have to validate the value, but we'd have to do it on each platform (assuming uint is valid for _any_ platform; I'm not sure if iOS can really have a text field of > 2,147,483,647 characters). The uint set in Forms might be greater than the maximum possible value for UWP and Android.

So int lets us check in one place, uint makes us check in multiple places.

Does 0 = Unlimited apply here, or are we going to Int.MaxValue ?
Also shouldn't this enhancement also be applied to the Editor control as well?

OK so int would make more sense?
And how about the default value? 0 for 'unlimited'?

Ah @CZEMacLeod just beat me to it ;)

I think int.MaxValue as the default would work fine; that's effectively "unlimited" (insofar as we aren't the ones limiting it - the native controls are).

Also shouldn't this enhancement also be applied to the Editor control as well?

Ideally. Which means that the BindableProperty could be put on InputView so we don't have to repeat it.

Was this page helpful?
0 / 5 - 0 ratings