Commit 60f02feb by Will Daly

Merge pull request #5716 from edx/will/logistration-custom-error-msgs

Add field from server to set custom error messages for logistration fields
parents 3996b0a7 ac2201a6
......@@ -125,7 +125,7 @@ class FormDescription(object):
def add_field(
self, name, label=u"", field_type=u"text", default=u"",
placeholder=u"", instructions=u"", required=True, restrictions=None,
options=None
options=None, error_messages=None
):
"""Add a field to the form description.
......@@ -158,6 +158,11 @@ class FormDescription(object):
and `display_name` is the name to display to the user.
If the field type is "select", you *must* provide this kwarg.
error_messages (dict): Custom validation error messages.
Currently, the only supported key is "required" indicating
that the messages should be displayed if the user does
not provide a value for a required field.
Raises:
InvalidFieldError
......@@ -177,7 +182,8 @@ class FormDescription(object):
"placeholder": placeholder,
"instructions": instructions,
"required": required,
"restrictions": {}
"restrictions": {},
"errorMessages": {},
}
if field_type == "select":
......@@ -201,6 +207,9 @@ class FormDescription(object):
)
raise InvalidFieldError(msg)
if error_messages is not None:
field_dict["errorMessages"] = error_messages
# If there are overrides for this field, apply them now.
# Any field property can be overwritten (for example, the default value or placeholder)
field_dict.update(self._field_overrides.get(name, {}))
......@@ -228,6 +237,7 @@ class FormDescription(object):
{"value": "wine", "name": "Wine"}
]
"restrictions": {},
"errorMessages": {},
},
{
"name": "comments",
......@@ -240,6 +250,7 @@ class FormDescription(object):
"restrictions": {
"max_length": 200
}
"errorMessages": {},
},
...
]
......
......@@ -87,6 +87,9 @@ class FormDescriptionTest(TestCase):
restrictions={
"min_length": 2,
"max_length": 10
},
error_messages={
"required": "You must provide a value!"
}
)
......@@ -105,6 +108,9 @@ class FormDescriptionTest(TestCase):
"restrictions": {
"min_length": 2,
"max_length": 10,
},
"errorMessages": {
"required": "You must provide a value!"
}
}
]
......
......@@ -378,37 +378,41 @@ class RegistrationView(APIView):
# Translators: This is a legal document users must agree to in order to register a new account.
terms_text = _(u"Terms of Service and Honor Code")
# Translators: "Terms of service" is a legal document users must agree to in order to register a new account.
label = _(
u"I agree to the {terms_of_service}"
).format(
terms_of_service=u"<a href=\"{url}\">{terms_text}</a>".format(
url=marketing_link("HONOR"),
terms_text=terms_text
)
terms_link = u"<a href=\"{url}\">{terms_text}</a>".format(
url=marketing_link("HONOR"),
terms_text=terms_text
)
# Translators: "Terms of service" is a legal document users must agree to in order to register a new account.
label = _(u"I agree to the {terms_of_service}").format(terms_of_service=terms_link)
# Translators: "Terms of service" is a legal document users must agree to in order to register a new account.
error_msg = _(u"You must agree to the {terms_of_service}").format(terms_of_service=terms_link)
form_desc.add_field(
"honor_code",
label=label,
field_type="checkbox",
default=False,
required=required,
error_messages={
"required": error_msg
}
)
def _add_terms_of_service_field(self, form_desc, required=True):
# Translators: This is a legal document users must agree to in order to register a new account.
terms_text = _(u"Terms of Service")
terms_link = u"<a href=\"{url}\">{terms_text}</a>".format(
url=marketing_link("TOS"),
terms_text=terms_text
)
# Translators: "Terms of service" is a legal document users must agree to in order to register a new account.
label = _(
u"I agree to the {terms_of_service}"
).format(
terms_of_service=u"<a href=\"{url}\">{terms_text}</a>".format(
url=marketing_link("TOS"),
terms_text=terms_text
)
)
label = _(u"I agree to the {terms_of_service}").format(terms_of_service=terms_link)
# Translators: "Terms of service" is a legal document users must agree to in order to register a new account.
error_msg = _("You must agree to the {terms_of_service}").format(terms_of_service=terms_link)
form_desc.add_field(
"terms_of_service",
......@@ -416,6 +420,9 @@ class RegistrationView(APIView):
field_type="checkbox",
default=False,
required=required,
error_messages={
"required": error_msg
}
)
def _options_with_default(self, options):
......
Markdown is supported
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