Commit ac2201a6 by Will Daly

Add field from server to set custom error messages for logistration fields

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