Issue with custom validator and input field

Hello !
We use TMPro for a while now and we were in 1.3.0 and we updated to 1.4.1 but now some fields doesn’t work in our project.
We have a custom validator to remove emoji from our field with the following Validate method :

using System;
using TMPro;
using UnityEngine;

[Serializable]
public class NoEmojiValidator : TMP_InputValidator
{
    private TMP_InputField InputField { get; set; }

    private int m_startSelectIndex = 0;
    private int m_endSelectIndex = 0;

    private bool IsSelected { get { return m_startSelectIndex != m_endSelectIndex; } }

    public void Init(TMP_InputField inputField)
    {
        InputField = inputField;
        InputField.onTextSelection.AddListener(OnTextSelection);
        InputField.onEndTextSelection.AddListener(OnEndTextSelection);
    }

    private void OnDestroy()
    {
        InputField.onTextSelection.RemoveListener(OnTextSelection);
        InputField.onEndTextSelection.RemoveListener(OnEndTextSelection);
    }

    private void OnTextSelection(string text, int startSelectionIndex, int endSelectionIndex)
    {
        m_startSelectIndex = startSelectionIndex;
        m_endSelectIndex = endSelectionIndex;
    }

    private void OnEndTextSelection(string s, int startSelectionIndex, int endSelectionIndex)
    {
        m_startSelectIndex = startSelectionIndex;
        m_endSelectIndex = endSelectionIndex;
    }

    public override char Validate(ref string text, ref int pos, char ch)
    {
        if ((int)ch >= 55357)
        {
            return '\0';
        }
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
        if (IsSelected)
        {
            int minIndex = Mathf.Min(m_startSelectIndex, m_endSelectIndex);
            int maxIndex = Mathf.Max(m_startSelectIndex, m_endSelectIndex);
            int length = maxIndex - minIndex;

            text = text.Remove(minIndex, length);
            text = text.Insert(minIndex, ch.ToString());
        }
        else
        {
            text = text.Insert(pos, ch.ToString());
        }
#endif
        pos += 1;
        return ch;
    }
}

I think our issue come from here cause when we use any other “Character Validation” all is fine. But as soon as we use this particular validator it fails whereas it was ok before.

I continue to search where is the problem but if you have any hint or if it’s a bug from your side, thanks in advance to answer :slight_smile:

P.S : This is our issue =>

IndexOutOfRangeException: Index was outside the bounds of the array.
TMPro.TMP_InputField.GenerateCaret (UnityEngine.UI.VertexHelper vbo, UnityEngine.Vector2 roundingOffset) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3313)
TMPro.TMP_InputField.OnFillVBO (UnityEngine.Mesh vbo) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3281)
TMPro.TMP_InputField.UpdateGeometry () (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3219)
TMPro.TMP_InputField.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3194)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:198)
UnityEngine.Canvas:SendWillRenderCanvases()

I can see in that case your “caretPositionInternal” has a bad value : -1

1 Like

Can you provide me with a simple repro project / scene to test this?

You can always submit a bug report with the included project and then provide me with the Case #.

Hello @Stephan_B
I made a really super simple repro project in that case : #1172102

I really hope you will find why it’s not working since our last update :slight_smile:

I update my ticket cause it doesn’t work with Unity 2019 and TextMeshPro 2.0.1

@Stephan_B sorry to insiste but I didn’t have any return from the QA team (whereas it’s quite fast most of time). Any news on this issue ?

I’ll try taking a look at it tomorrow and provide feedback shortly thereafter.

1 Like

I am currently looking at the case and it appears there is an extra null character that is causing the issue with your custom validator.

QA has already replied to the case and suggested a code change which will allow you to get around the reported issue which is to simply check for char(0).

I’ll take a close look on my end to figure out what is going on with this null character.

I was able to identify the source of the Null character being forwarded to the Validate method.

This behavior will be as it was in version 1.3.0 of the TMP package. The solution for this issue will be in the next release of the TMP package which is expected to be version 1.4.2 for Unity 2018.3 or newer and version 2.0.2 for Unity 2019.x.

Until the next package is available, just filter out the Null character as per the QA’s suggestion.

1 Like

Yeah ! Thanks again for the really fast answer and fix @Stephan_B and to the QA Team !

As usual you are most welcome :slight_smile:

@Stephan_B I was maybe too enthusiastic cause it seems it don’t work on mobile devices.
All our custom validator using the one mentionned above doesn’t take any input on mobile. So We can see the input action (the field enter in a selected state) but the native mobile keyboard never shows up.

Everything is fine on Standalone and Editor probably due to the no-keyboard.

I try to investigate further… but it’s quite a major issue for us ^^"

1 Like

Can you submit a new bug report with a project / scene that will enable me to re-test one of your custom validator?

I think the case 1172102 must (still open) must be enough to repro this issue :wink:
But I can submit again exactly the same with default target platform to android or iOS :slight_smile:

You just need to add the QA fix in it. But I can understand that you need it for the tracking.

Actually reflecting on this last night as I went to sleep, I realized this was related to the following:

In the TMP_InputField.cs at line 3717, make the following changes

// Replace the following line with the line below
if (shouldHideSoftKeyboard == false && m_ReadOnly == false && contentType != ContentType.Custom)

// New line should be
if (shouldHideSoftKeyboard == false && m_ReadOnly == false)

That should solve the issue.

2 Likes

Ok I will check this tomorrow morning !
Any ETA for the 1.4.2 release with all those fixes ?

It will be version 1.5.0 and it is week to week now. Hoping to release a preview next week.

1 Like

I just test it and it works. The keyboard appears again after your fix.

1 Like

This workaround was still required for me in Jan 2020

Yup it’s still working. Thanks a lot Stephan! :slight_smile: You save my day

Thank you! Dumb question but where is the TMP_InputField.cs script? I can’t find it in the “TextMesh Pro” folder in my project “Assets” folder. Definitely imported TMP into my project with no problem.