Skip to content

Commit

Permalink
Prevent invalid characters being entered at the appropriate times
Browse files Browse the repository at this point in the history
When a validator does not allow for certain characters to be entered,
then it should not allow these to be entered in even if an input mask
is set. This fixes a regression introduced in
1b21b73.

The test modified is because this is in fact a general limitation when
combining validators and input masks, when a separator is used.
Whereas the original patch did allow this to be possible, this is now
not possible again.

Task-number: QTBUG-64616
Change-Id: Ic6a3f40a9faa7c04abc055cfc2752044fddd33a0
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Reviewed-by: Robin Burchell <robin.burchell@crimson.no>
  • Loading branch information
AndyShawQt committed Feb 2, 2018
1 parent 9c08bd6 commit 5a10cf0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
36 changes: 9 additions & 27 deletions src/quick/items/qquicktextinput.cpp
Expand Up @@ -1077,7 +1077,8 @@ void QQuickTextInputPrivate::checkIsValid()
Q_Q(QQuickTextInput);

ValidatorState state = hasAcceptableInput(m_text);
m_validInput = state != InvalidInput;
if (!m_maskData)
m_validInput = state != InvalidInput;
if (state != AcceptableInput) {
if (m_acceptableInput) {
m_acceptableInput = false;
Expand Down Expand Up @@ -3530,11 +3531,15 @@ bool QQuickTextInputPrivate::finishChange(int validateFromState, bool update, bo
#if QT_CONFIG(validator)
if (m_validator) {
QString textCopy = m_text;
if (m_maskData)
textCopy = maskString(0, m_text, true);
int cursorCopy = m_cursor;
QValidator::State state = m_validator->validate(textCopy, cursorCopy);
if (m_maskData)
textCopy = m_text;
m_validInput = state != QValidator::Invalid;
m_acceptableInput = state == QValidator::Acceptable;
if (m_validInput) {
if (m_validInput && !m_maskData) {
if (m_text != textCopy) {
internalSetText(textCopy, cursorCopy);
return true;
Expand All @@ -3543,31 +3548,8 @@ bool QQuickTextInputPrivate::finishChange(int validateFromState, bool update, bo
}
}
#endif

if (m_maskData) {
m_validInput = true;
if (m_text.length() != m_maxLength) {
m_validInput = false;
m_acceptableInput = false;
} else {
for (int i = 0; i < m_maxLength; ++i) {
if (m_maskData[i].separator) {
if (m_text.at(i) != m_maskData[i].maskChar) {
m_validInput = false;
m_acceptableInput = false;
break;
}
} else {
if (!isValidInput(m_text.at(i), m_maskData[i].maskChar)) {
m_acceptableInput = false;
if (m_text.at(i) != m_blank)
m_validInput = false;
break;
}
}
}
}
}
if (m_maskData)
checkIsValid();

if (validateFromState >= 0 && wasValidInput && !m_validInput) {
if (m_transactions.count())
Expand Down
12 changes: 10 additions & 2 deletions tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp
Expand Up @@ -6143,9 +6143,17 @@ void tst_qquicktextinput::keypress_inputMask_withValidator_data()
KeyList keys;
// inserting '1111.11' then two backspaces
keys << Qt::Key_Home << "1111.11" << Qt::Key_Backspace << Qt::Key_Backspace;
QTest::newRow("backspaceWithRegExp") << QString("9999.99;_") << 0.0 << 0.0 << 0
QTest::newRow("backspaceWithRegExp") << QString("9999;_") << 0.0 << 0.0 << 0
<< QString("/^[-]?((\\.\\d+)|(\\d+(\\.\\d+)?))$/")
<< keys << QString("1111.") << QString("1111.__");
<< keys << QString("11") << QString("11__");
}
{
KeyList keys;
// inserting '99' - QTBUG-64616
keys << Qt::Key_Home << "99";
QTest::newRow("invalidTextWithRegExp") << QString("X9;_") << 0.0 << 0.0 << 0
<< QString("/[+-][0+9]/")
<< keys << QString("") << QString("__");
}
}

Expand Down

0 comments on commit 5a10cf0

Please sign in to comment.