Search code examples
pythonweb2py

Is it possible to wrap DIV´s around form-elements in web2py controller


When you define a form in the controller like:

form = crud.update(db.my_table, my_id)

is it possible to simply wrap an extra DIV around certain form-elements, in this case Inputs:

for input in form.elements('input'):
    wrap_div_around(input)

and update the form with the wrapped input?


Solution

  • You can use the replace argument to .elements(), which can be a function that takes the original element and returns a new element:

    form.elements('input', replace=wrap_div_around)
    

    You might also consider creating a custom formstyle function that generates the exact form markup you are looking for. For examples of formstyle functions, see https://github.com/web2py/web2py/blob/ad43249f6148402890077a0a649e46b42fa6a8ab/gluon/sqlhtml.py#L886-L1083.