I like this. In trunk now.

On Feb 27, 12:46 pm, teemu <[email protected]> wrote:
> Hi,
>
> I would like to suggest some changes to SQLFORM class. I heavily use
> 'formstyle=function' feature in my web2py-projects to create
> customized form layouts. However, there is one big disadvantage in
> behavior of the standard SQLFORM code. SQLFORM always wraps form into
> <table> element if one define 'formstyle=function'. Standard
> formstyles 'divs' and 'ul' don't use <table> element at all but wraps
> form to DIV and UL elements respectively. This is code from
> web2py.gluon.sqlhtml:
> ----------------------------------------
> clip----------------------------------------------
>         elif type(formstyle) == type(lambda:None):
>             table = TABLE()
>             for id,a,b,c in xfields:
>                 td_b = self.field_parent[id] = TD(b,_class='w2p_fw')
>                 newrows = formstyle(id,a,td_b,c)
>                 if type(newrows).__name__ != "tuple":
>                     newrows = [newrows]
>                 for newrow in newrows:
>                     table.append(newrow)
> ----------------------------------------
> clip----------------------------------------------
>
> I know that it is not possible to change standard behavior of the
> current code because it would break backward compatibility. I have
> tried to think about other possibilities to modify the SQLFORM code to
> bring in some more flexibility or even full freedom to define form
> layouts. Currently it is not easy to change behavior of the code by
> extending SQLFORM class because the code that I would like to change
> is part of SQLFORM constructor. I would need to replace whole
> constructor that is quite a big bunch of code.
>
> This is my suggestion:
> It would easier to override standard behavior if formstyle code were
> outside of constructor in it's own function. Then I would need to
> override only this particular function. I created this quick hack
> patch to illustrate my idea. What do you thing Massimo? Would this be
> the right way to go or do you have better suggestions?
>
> --------------------------------------
> clip----------------------------------------------
> # HG changeset patch
> # User teemu <[email protected]>
> # Date 1298831231 -7200
> # Node ID b44d92468fd59d7197756d5e02493a0bb6a8e8b1
> # Parent  05c759fbff4a6dcba140f7f6c59a5cb0e583e73a
> customformstyle
>
> diff -r 05c759fbff4a -r b44d92468fd5 gluon/sqlhtml.py
> --- a/gluon/sqlhtml.py  Fri Feb 25 22:13:51 2011 -0600
> +++ b/gluon/sqlhtml.py  Sun Feb 27 20:27:11 2011 +0200
> @@ -891,46 +891,51 @@
>          (begin, end) = self._xml()
>          self.custom.begin = XML("<%s %s>" % (self.tag, begin))
>          self.custom.end = XML("%s</%s>" % (end, self.tag))
> -        if formstyle == 'table3cols':
> +       table = self.createform(xfields)
> +        self.components = [table]
> +
> +    def createform(self, xfields):
> +        if self.formstyle == 'table3cols':
>              table = TABLE()
>              for id,a,b,c in xfields:
>                  td_b = self.field_parent[id] = TD(b,_class='w2p_fw')
>                  table.append(TR(TD(a,_class='w2p_fl'),
>                                  td_b,
>                                  TD(c,_class='w2p_fc'),_id=id))
> -        elif formstyle == 'table2cols':
> +        elif self.formstyle == 'table2cols':
>              table = TABLE()
>              for id,a,b,c in xfields:
>                  td_b = self.field_parent[id] =
> TD(b,_class='w2p_fw',_colspan="2")
>                  table.append(TR(TD(a,_class='w2p_fl'),
>                                  TD(c,_class='w2p_fc'),_id=id
> +'1',_class='even'))
>                  table.append(TR(td_b,_id=id+'2',_class='odd'))
> -        elif formstyle == 'divs':
> +        elif self.formstyle == 'divs':
>              table = TAG['']()
>              for id,a,b,c in xfields:
>                  div_b = self.field_parent[id] =
> DIV(b,_class='w2p_fw')
>                  table.append(DIV(DIV(a,_class='w2p_fl'),
>                                   div_b,
>                                   DIV(c,_class='w2p_fc'),_id=id))
> -        elif formstyle == 'ul':
> +        elif self.formstyle == 'ul':
>              table = UL()
>              for id,a,b,c in xfields:
>                  div_b = self.field_parent[id] =
> DIV(b,_class='w2p_fw')
>                  table.append(LI(DIV(a,_class='w2p_fl'),
>                                   div_b,
>                                   DIV(c,_class='w2p_fc'),_id=id))
> -        elif type(formstyle) == type(lambda:None):
> +        elif type(self.formstyle) == type(lambda:None):
>              table = TABLE()
>              for id,a,b,c in xfields:
>                  td_b = self.field_parent[id] = TD(b,_class='w2p_fw')
> -                newrows = formstyle(id,a,td_b,c)
> +                newrows = self.formstyle(id,a,td_b,c)
>                  if type(newrows).__name__ != "tuple":
>                      newrows = [newrows]
>                  for newrow in newrows:
>                      table.append(newrow)
>          else:
>              raise RuntimeError, 'formsyle not supported'
> -        self.components = [table]
> +
> +       return table
>
> --------------------------------------
> clip----------------------------------------------
>
> I had to pass xfields to function (createform(xfields)) because
> otherwise it is not accessible outside of the constructor. Would it be
> possible to change xfield to self.xfields?
>
> Teemu

Reply via email to