[SOLVED] How to use non ascii values in SimpleTerm vocabularies for required schema.Choices in z3c.form in Plone 4.3.14?

Take the following snippet of code:


field_vocabulary = SimpleVocabulary(
    [SimpleTerm(value=u'Apresentação'.encode('utf8'), title='Apresentação'),
     SimpleTerm(value=u'Relatório'.encode('utf8'), title='Relatório'),
     ])

(...)

field = schema.Choice(
        title=_(u'field_title'),
        description=_(u'field_description'),
        required=True,
        vocabulary=field_vocabulary,
    )

Problem is: since this field is required, it never validates because u'Apresentação is non ascii and in https://github.com/zopefoundation/z3c.form/blob/3.2.11/src/z3c/form/validator.py#L48 value comes as None!, if I change it for u'Apresentacao it works.

@mauritsvanrees or @hvelarde, have you ever come across this kind of problem?

EDIT: The problem seems worse: if it's not required and the value of my SimpleTerm is non-ascii, it's persisted as None.

Non-ascii values should still have ascii token for SimpleTerm(value=, token=, title=).

You could possible use id normalizer from plone.i18n to make ascii versions.

1 Like

Just check https://docs.plone.org/develop/plone/forms/vocabularies.html#vocabulary-terms. Any reason why it must be ascii? Why schema.TextLine can have non ascii and a SimpleTerm can't in it's token?

it has to be ASCII because that's the way it is; check the following code (but use plone.i18n instead):

Having the token with ascii, I can have a non ascii value with no issues. Thanks @datakurre.

Not really the answer we were looking for but truth is always hard. :laughing:

1 Like