Commit b1ebbec8 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Allow alternative error messages for assistive tools

This allows to set alternative error messages for use by assistive tools
if the displayed error messages are not suitable.

Moreover, the error messages are made more specific and are worded as
instructions if appropriate.

GnuPG-bug-id: 5916
parent 82aa98db
......@@ -89,14 +89,17 @@ public:
ui.nameInput = FormTextInput<QLineEdit>::create(q);
ui.nameInput->setLabelText(i18nc("@label", "Name"));
ui.nameInput->setIsRequired(nameIsRequired);
ui.nameInput->setValueRequiredErrorMessage(i18n("Error: The name is required."));
ui.nameInput->setValueRequiredErrorMessage(i18n("Error: Enter a name."));
const auto regexp = config.readEntry("NAME_regex");
if (regexp.isEmpty()) {
ui.nameInput->setValidator(Validation::simpleName(Validation::Optional, q));
ui.nameInput->setHint(i18n("Must not include &lt;, &gt;, and @."),
i18nc("text for screen readers",
"Must not include less-than sign, greater-than sign, and at sign."));
ui.nameInput->setInvalidEntryErrorMessage(i18n("Error: The entered name contains invalid characters."));
ui.nameInput->setInvalidEntryErrorMessage(
i18n("Error: The name must not include &lt;, &gt;, and @."),
i18nc("text for screen readers",
"Error: The name must not include less-than sign, greater-than sign, and at sign."));
} else {
ui.nameInput->setValidator(Validation::simpleName(regexp, Validation::Optional, q));
ui.nameInput->setHint(i18n("Must be in the format required by your organization and "
......@@ -104,9 +107,12 @@ public:
i18nc("text for screen readers",
"Must be in the format required by your organization and "
"must not include less-than sign, greater-than sign, and at sign."));
ui.nameInput->setInvalidEntryErrorMessage(i18n(
"Error: The entered name contains invalid characters "
"or it does not follow your organization's rules."));
ui.nameInput->setInvalidEntryErrorMessage(
i18n("Error: The name must be in the format required by your organization and "
"it must not include &lt;, &gt;, and @."),
i18nc("text for screen readers",
"Error: The name must be in the format required by your organization and "
"it must not include less-than sign, greater-than sign, and at sign."));
}
mainLayout->addWidget(ui.nameInput->label());
......@@ -121,18 +127,18 @@ public:
ui.emailInput = FormTextInput<QLineEdit>::create(q);
ui.emailInput->setLabelText(i18nc("@label", "Email address"));
ui.emailInput->setIsRequired(emailIsRequired);
ui.emailInput->setValueRequiredErrorMessage(i18n("Error: The email address is required."));
ui.emailInput->setValueRequiredErrorMessage(i18n("Error: Enter an email address."));
const auto regexp = config.readEntry(QLatin1String("EMAIL_regex"));
if (regexp.isEmpty()) {
ui.emailInput->setValidator(Validation::email(Validation::Optional, q));
ui.emailInput->setInvalidEntryErrorMessage(i18n("Error: The entered email address is not valid."));
ui.emailInput->setInvalidEntryErrorMessage(i18n(
"Error: Enter an email address in the correct format, like name@example.com."));
} else {
ui.emailInput->setValidator(Validation::email(regexp, Validation::Optional, q));
ui.emailInput->setHint(i18n(
"Must be in the format required by your organization"));
ui.emailInput->setInvalidEntryErrorMessage(i18n(
"Error: The entered email address is not valid "
"or it does not follow your organization's rules."));
"Error: Enter an email address in the correct format required by your organization."));
}
mainLayout->addWidget(ui.emailInput->label());
......
......@@ -25,12 +25,12 @@ namespace
{
auto defaultValueRequiredErrorMessage()
{
return i18n("Error: A value is required.");
return i18n("Error: Enter a value.");
}
auto defaultInvalidEntryErrorMessage()
{
return i18n("Error: The entered text is not valid.");
return i18n("Error: Enter a value in the correct format.");
}
}
......@@ -59,6 +59,7 @@ public:
void setLabelText(const QString &text, const QString &accessibleName);
void setHint(const QString &text, const QString &accessibleDescription);
QString errorMessage(Error error) const;
QString accessibleErrorMessage(Error error) const;
void updateError();
void updateAccessibleNameAndDescription();
......@@ -71,7 +72,9 @@ public:
QString mAccessibleName;
QString mAccessibleDescription;
QString mValueRequiredErrorMessage;
QString mAccessibleValueRequiredErrorMessage;
QString mInvalidEntryErrorMessage;
QString mAccessibleInvalidEntryErrorMessage;
Error mError = EntryOK;
bool mRequired = false;
bool mEditingInProgress = false;
......@@ -123,6 +126,19 @@ QString FormTextInputBase::Private::errorMessage(Error error) const
return {};
}
QString FormTextInputBase::Private::accessibleErrorMessage(Error error) const
{
switch (error) {
case EntryOK:
return {};
case EntryMissing:
return mAccessibleValueRequiredErrorMessage;
case InvalidEntry:
return mAccessibleInvalidEntryErrorMessage;
}
return {};
}
void FormTextInputBase::Private::updateError()
{
if (!mErrorLabel) {
......@@ -153,6 +169,7 @@ void FormTextInputBase::Private::updateError()
}
mErrorLabel->setVisible(!newErrorMessage.isEmpty());
mErrorLabel->setText(newErrorMessage);
mErrorLabel->setAccessibleName(accessibleErrorMessage(mError));
updateAccessibleNameAndDescription();
}
......@@ -169,7 +186,7 @@ void FormTextInputBase::Private::updateAccessibleNameAndDescription()
// Qt does not support "described-by" relations (like WCAG's "aria-describedby" relationship attribute);
// emulate this by adding the error message to the accessible description of the input field
const auto description = errorShown ? mAccessibleDescription + QLatin1String{" "} + mErrorLabel->text()
const auto description = errorShown ? mAccessibleDescription + QLatin1String{" "} + mErrorLabel->accessibleName()
: mAccessibleDescription;
if (mWidget && mWidget->accessibleDescription() != description) {
mWidget->setAccessibleDescription(description);
......@@ -245,22 +262,32 @@ void FormTextInputBase::setValidator(const QValidator *validator)
d->mValidator = validator;
}
void FormTextInputBase::setValueRequiredErrorMessage(const QString &text)
void FormTextInputBase::setValueRequiredErrorMessage(const QString &text, const QString &accessibleText)
{
if (text.isEmpty()) {
d->mValueRequiredErrorMessage = defaultValueRequiredErrorMessage();
} else {
d->mValueRequiredErrorMessage = text;
}
if (accessibleText.isEmpty()) {
d->mAccessibleValueRequiredErrorMessage = d->mValueRequiredErrorMessage;
} else {
d->mAccessibleValueRequiredErrorMessage = accessibleText;
}
}
void FormTextInputBase::setInvalidEntryErrorMessage(const QString &text)
void FormTextInputBase::setInvalidEntryErrorMessage(const QString &text, const QString &accessibleText)
{
if (text.isEmpty()) {
d->mInvalidEntryErrorMessage = defaultInvalidEntryErrorMessage();
} else {
d->mInvalidEntryErrorMessage = text;
}
if (accessibleText.isEmpty()) {
d->mAccessibleInvalidEntryErrorMessage = d->mInvalidEntryErrorMessage;
} else {
d->mAccessibleInvalidEntryErrorMessage = accessibleText;
}
}
void FormTextInputBase::setToolTip(const QString &toolTip)
......
......@@ -87,17 +87,21 @@ public:
void setValidator(const QValidator *validator);
/**
* Sets the error message to display if a value is required for this input field,
* but if no value has been entered. If \p text is empty, then a default
* message will be used.
* Sets \p text as error message to display if a value is required for the
* input field, but if no value has been entered. If \p text is empty, then
* a default message will be used.
* The optional \p accessibleText is used as alternative text for assistive
* tools.
*/
void setValueRequiredErrorMessage(const QString &text);
void setValueRequiredErrorMessage(const QString &text, const QString &accessibleText = {});
/**
* Sets the error message to display if the entered value is not accepted
* Sets \p text as error message to display if the entered value is not accepted
* by the validator. If \p text is empty, then a default message will be used.
* The optional \p accessibleText is used as alternative text for assistive
* tools.
*/
void setInvalidEntryErrorMessage(const QString &text);
void setInvalidEntryErrorMessage(const QString &text, const QString &accessibleText = {});
/**
* Sets the tool tip of the controlled widget and its associated label.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment