[Zope-dev] Decimal validation error.

Hermann Himmelbauer dusty at qwer.tk
Wed Feb 17 18:17:14 EST 2010


Am Mittwoch 17 Februar 2010 21:37:17 schrieb Mats Nordgren:
> Thanks Hermann,
>
> This still does not work for me.  The pattern = '#,##0.####' seems to have
> no effect on the converter. Perhaps I'm doing something wrong, I'm posting
> my setup below.
>
> This most certainly have to be a bug in the system if you can't put 1.2345
> into a decimal field, no?  If so is there a bug tracker for z3c.form where
> I can post this and get it fixed?
>
> #interfaces.py
> class IFixedDecimal(IDecimal):
>     """Fixed decimal field."""
>
> class FixedDecimal(schema.Decimal):
>     interface.implements(
> IFixedDecimal)
>
> class ITaxLot(interface.Interface):
>     """A tax lot"""
>
>     quantity = FixedDecimal(
>         title = u'Quantity',
>         required = True)
>
> #holding.py
> class MyDecimalDataConverter(DecimalDataConverter):
>     """Adapt decimal data converter"""
>     adapts(IFixedDecimal, IWidget)
>
>     # The amount of '#' after the '.' outlines the no. of decimal places
>     pattern = '#,##0.####'
>
> #configure.zcml
>
>   <includeOverrides file="overrides.zcml" />
>
> #overrides.zcml
>
>   <adapter factory=".holding.MyDecimalDataConverter" />

Ah, I forgot the method, try to add something like this to your converter:

def toFieldValue(self, value):
   try:
      return self.formatter.parse(value, pattern=self.pattern)
   except zope.i18n.format.NumberParseError, err:
      raise FormatterValidationError(self.errorMessage, value)

I'd furthermore suggest to add some debugging information into this method to 
make sure that it is actually called.

Best Regards,
Hermann


-- 
hermann at qwer.tk
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope-Dev mailing list