On Thu, Aug 5, 2010 at 7:12 PM, Chris S <[email protected]> wrote:
> Using a custom form in your view should not change your controller
> any.
>
> Just use the same controller and instead of the view having:
> {{=form}}
I don't have {{=form}} in my view. Here is the view:
http://pastebin.com/npCADyQs
> It should have:
> {{=form.custom.begin}}
> // your custom stuff including submit button
> {{=form.custom.end}}
When I do this, I get a double form (the autogenerated and the
custom), but custom doesn't work.
See this screenshot: http://img256.imageshack.us/img256/8779/sug3.png
If I remove form=form from the controller, I get an error:
Traceback (most recent call last):
File "/home/sbassi/Downloads/web2py/gluon/restricted.py", line 178,
in restricted
exec ccode in environment
File
"/home/sbassi/Downloads/web2py/applications/sug3/views/default/index.html",
line 68, in <module>
NameError: name 'form' is not defined
If you look at the official manual, here:
http://www.web2py.com/book/default/chapter/07
It says you should return an empty dictionary (copied from that URL):
SQLFORM in HTML
There are times when you want to use SQLFORM to benefit from its form
generation and processing, but you need a level of customization of
the form in HTML that you cannot achieve with the parameters of the
SQLFORM object, so you have to design the form using HTML.
Now, edit the previous controller and add a new action:
def display_manual_form():
form = SQLFORM(db.person)
if form.accepts(request.vars, formname='test'):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill the form'
return dict()
and insert the form in the associated "default/display_manual_form.html" view:
{{extend 'layout.html'}}
<form>
<ul>
<li>Your name is <input name="name" /></li>
</ul>
<input type="submit" />
<input type="hidden" name="_formname" value="test" />
</form>
Notice that the action does not return the form because it does not
need to pass it to the view. The view contains a form created manually
in HTML. The form contains a hidden field "_formname" that must be the
same formname specified as an argument of accepts in the action.
web2py uses the form name in case there are multiple forms on the same
page, to determine which one was submitted. If the page contains a
single form, you can set formname=None and omit the hidden field in
the view.