-
Notifications
You must be signed in to change notification settings - Fork 72
Closed
Description
When attempting to force a translation, the postal code format translator stays in English. This happens in both master and v1.2.6. Digging deeper, it looks like an issue with a chained validator:
Example code:
import peppercorn
class form(object):
def __init__(self, schema=None, decode=peppercorn.parse,
encode=utils.peppercorn_encode, validators=None, form=None,
state=None, on_get=False, **htmlfill_kwargs):
self.schema = schema
self.form = form
self.validators = validators
self.on_get = on_get
self.htmlfill_kw = htmlfill_kwargs.copy()
self.state = None
def validate_in_place(self):
errors = {}
result = None
params = self.decode(self.form.items())
if self.schema:
try:
result = self.schema.to_python(params, self.state)
except formencode.Invalid, e:
errors = e.unpack_errors()
log.debug("Errors in validating: %s", errors)
return errors
def validate(self, request, params):
errors = {}
log.debug("Calling validate on form %s", self)
if self.schema:
log.debug("Validating against a schema")
try:
request.form_result = self.schema.to_python(params, self.state)
except formencode.Invalid, e:
errors = e.unpack_errors()
return errors
class PostalCodeInCountryFormat(national.PostalCodeInCountryFormat):
def validate_python(self, fields_dict, state):
# The parent validates the format for the country but doesn't check for an empty
# entry.
super(PostalCodeInCountryFormat, self).validate_python(fields_dict, state)
# Check for an empty value here, but only for countries whose formats are
# validated.
if fields_dict[self.country_field] in self._vd:
if fields_dict[self.zip_field] in (None, ""):
message = self.message('empty', state)
log.debug("Postalcode validation error, message=%s, state=%s, country_field=%s, zip_field=%s",
message, state, self.country_field, self.zip_field
)
raise formencode.Invalid(message, fields_dict, state,
error_dict = {self.zip_field: message})
class SMStringValidator(validators.String):
""" Custom string validator to take into account that a string of spaces is empty.
"""
only_spaces_regex = re.compile('[^\s]{1,}')
def validate_python(self, value, state):
super(SMStringValidator, self).validate_python(value, state)
#if value contains only white-space, disallow it.
if not self.only_spaces_regex.match(value):
raise formencode.Invalid(
self.message('empty', state),
value, state
)
class MySchema(formencode.Schema):
allow_extra_fields = True
validate_partial_form = True
postalcode = SMStringValidator(not_empty=False)
chained_validators = [PostalCodeInCountryFormat(zip_field='postalcode')]
...
formencode.api.set_stdtranslation(domain="FormEncode", languages=['de'])
data = {'postalcode': 333, 'country': 'CA'}
schema = form(schema=MySchema(), form=data)
errors = schema.validate_in_place()Result:
{
"country": "Die eingegebene Postleitzahl ist nicht im landesspezifischen Format.",
"postalcode": "Please enter a zip code (LnL nLn)"
}Any help would be greatly appreciated.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels