This is the style:
def formstyle_stardom(form, fields):
parent = CAT()
for fieldname in form.fields:
field = form.table[fieldname]
if field.type != "id":
parent += LABEL(fieldname, _for=fieldname) + INPUT(_name=fieldname, value=field.default)
parent += INPUT(_type="submit")
return parent
response.formstyle = formstyle_stardom
at the controller:
form = SQLFORM(db.reservas).process()
at the view:
{{=form}}
Nothing special,
im not using "for id, label, control, help in fields:" since i would like to build the controls, but if i use the parameter "form" to create custom markup, the form will skip validation. When i use the "fields" parameter the form behaves correctly.
The question is, if the form is passed as a parameter why cant we use it to create the style? Am i missing something? How is it posible that the form skips validation?
Thanks.
def mystyle(form):
container = CAT()
for fld in form.fields:
container += INPUT(_name=fld)
container += INPUT(_type="submit")
return CAT(form.custom.begin, container, form.custom.end)
{{=mystyle(form)}}
Im trying to create a formstyle, when the form is submitted without introducing any value it skips any kind of validation and, without being accepted, tries to perform db changes. Eventually i get an error ticket like:pymysql.err.InternalError'> (1048, u"Column 'salida' cannot be null")
This is the style:
def formstyle_stardom(form, fields):
parent = CAT()
for fieldname in form.fields:
field = form.table[fieldname]
if field.type != "id":
parent += LABEL(fieldname, _for=fieldname) + INPUT(_name=fieldname, value=field.default)
INPUT(_name=fieldname, value=field.default, requires=field.requires)
If you use the "controls" object passed into the formstyle function as part of the "fields" object, this is done for you automatically.I thought requires was performed in a different process than the form serialization. It would be better if the requires information isnt embeded in the inputs, but the form itself.
Wasnt expecting to redeclare requires as they already are at the models. In any case, that would do the trick.
I thought about it but it has a disadvantage. The SQLFORM is perfect to render complex queries without having to hardcode the html. If the models changes i dont have to worry about the view.
And this is where the bootstrap decouple comes in place. Everything is easy if you use the welcome app as layout but for custom markup theres not many examples on how to do things.
If its not too much trouble i have another question:
The form style isnt aware of form errors so i cannot "style" the form properly.
When i print form.errors inside the style i get an empty storage even when there are errors.
Is there any particular way to achieve this, using formstyles?
you should instead hide the errors, and then you could add your own via server-side DOM manipulation.
you should instead hide the errors, and then you could add your own via server-side DOM manipulation.
But how do i do that if i dont know when errors has happened?.
Currently the form gets automatic markup when is instantiated and its modified later on when process is called.
But this is like executing the view before the controller.
Wouldnt be better to serialize the form just once, when xml() is called?, even perfomance wise since nothings gets serialized until its droped into a view.
Either i have to go back to my workaround {{=mystyle(form)}} or rearrange the SQLFORM code so gets serialized at the end of its lifespan, like it should, in my opinion.
So far ive overriden "def xml(self):" to something like
return form.custom.begin + self.createform(xfields=None) + form.custom.end
def stylefrm(self):
#create any custom header or form.custom.begin
frm = FORM(_class="myform")
#iterate over the fields and create any behaviour and style you like
for fld in self.fields:
field = self.table[fld] #access the SQLFORM to inspect field properties
#implement keepvalues
if self.keepvalues:
out = LABEL(fld, _for=fld) + INPUT(_name=fld, requires=field.requires, _value=self.vars[fld])
else:
out = LABEL(fld, _for=fld) + INPUT(_name=fld, requires=field.requires)
frm.append(out)
# add the hidden fields + token
frm.append(self.hidden_fields())
#add any submit button
frm.append(INPUT(_type="submit", _value="submit"))
return str(frm)
# replace web2py html serialization for your own
SQLFORM.xml = stylefrm
def index():
frm = SQLFORM.factory(
Field("name", "string")
, Field("quantity", "integer")
, Field("price", "double")
)
# remove SQLFORM factory auto id field
frm.fields.pop(0)
result = "Not submitted"
if frm.process(keepvalues=True).accepted:
result = "All good"
elif frm.errors:
result = "Not good"
return locals()
View:
{{=frm}}
{{=result}}
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.