[Zope3-Users] zope.formlib doesn't check invariants on subschemas - need workaround

Rudá Porto Filgueiras rudazz at gmail.com
Mon Feb 4 17:08:05 EST 2008


On Feb 2, 2008 1:44 PM, Derek Richardson <derek.richardson at gatech.edu> wrote:
> I have a schema, let's call it B. It contains an @invariant function. I
> observed that it is not being called during form validation.
>
> After help from Philipp and some investigation, I determined that the
> problem is that schema B isn't itself the main form, but is nested
> within schema A as the value_type of a Tuple field. When schema A is
> validated, schema B's invariant is not checked. I think this may be a
> bug. If someone more knowledgeable will confirm, then I will open a bug
> report.
>
> In the mean time, I need a workaround that verifies that the invariants
> on all instances of schema B in the Tuple on schema A hold during form
> validation. My nascent idea is to put an invariant on schema A that
> checks all the invariants on all schema B instances. However, I am not
> familiar enough with formlib internals to code this up without
> significant research. Can someone help me out with an implementation for
> this workaround or an idea for a different, better workaround?

I have a similar problem using Tuple with Choice. formlib validation
don't check is this field has a value. Is that a bug?

Then I create my own validate method and custom widget that follow:

    def validate(self, action, data):
        """Validate form using same as FormBase along with a
customized validation.
        """
        return (form.getWidgetsData(self.widgets, self.prefix, data)
                + form.checkInvariants(self.form_fields, data)
                + self.validate_enviar(action, data)
                )

    def validate_enviar(self, action, data):
        """Validation for field Tuple with value_type=Choice and my
own widget derived from MultiCheckBoxWidget
        """
        errors = []
        for form_field in self.form_fields:
            error = None
            field_name = form_field.__name__
            widget = self.widgets[field_name]
            empty_marker_name = getattr(widget, 'empty_marker_name', None)
            if widget.name not in self.request.form and empty_marker_name:
                if empty_marker_name not in self.request.form:
                    error = WidgetInputError(field_name=field_name,

widget_title=form_field.field.title,
                                             errors=('Campo
obrigatório não informado.')
                                             )
            if error:
                widget.error = error.errors
                errors.append(error)
        return errors

class MyMultiCheckBoxWidget(MultiCheckBoxWidget):
    """Subclass to customized MultiCheckBoxWidget
    """
    orientation = 'horizontal'

    def _emptyMarker(self):
        """Disable _empytMarker
        """
        return ''


> Thanks,
>
> Derek
>
> _______________________________________________
> Zope3-users mailing list
> Zope3-users at zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users
>



-- 
Rudá Porto Filgueiras
Weimar Consultoria

http://python-blog.blogspot.com

Hospedagem Plone, Zope e Python?
http://www.pytown.com


More information about the Zope3-users mailing list