Thursday 2009-05-28
This sections covers Views and SQLFORM()
I discovered that the underscore prefix on arguments in HTML helpers means
it is a user-defined attribute which will cause the control to emit an attribute
with that name but without the underscore prefix
So for example
{ {=LI(A(regionname.strRegionName, _href="#", _id="node_1"))} }
outputs
<a href="#" id="node_1">NorthEast</a>
Here, _id is not defined anywhere in the web2py source code
– it is user defined here on the spot.
So _id is "implicitly defined" (aka user-defined)
-----------------------------------------------------------
From
http://groups.google.com/group/web2py/browse_thread/thread/382f6d3e54e35935
From:
mgbeli...@gmail.com
Date: Thu, 14 May 2009 11:02:33 -0700 (PDT)
Local: Thurs, May 14 2009 2:02 pm
Subject: bug in form behavior?
Hello,
This is my first question on the mailing lists,
so I must say that
I like web2py a lot!
I've noticed some strange behavior with the form.vars,
I'll try to explain it with a quick example here in an
imaginary default.py controller.
<code>
# default.py controller.
def index():
form = FORM(
INPUT(_name='field'),
INPUT(_name='field'),
INPUT(_name='field'),
INPUT(_type='submit'),
)
#####
# So pretend I entered the form with 'John Smith'
# in the first input,
# 'Fred Smith' in the second input,
# and 'Bob Smith' in the third.
#####
if form.accepts(request.vars, session):
return len(form.vars.field)
elif form.errors:
return None
return dict(form=form)
</code>
I would expect form.vars.field to return
['John Smith', 'Fred Smith','Bob Smith']
but instead it returns the the representation
of the list as a string, in other words:
"['John Smith', 'Fred Smith', 'Bob Smith']".
Therefore, len(form.vars.field) returns 41, whereas you
would expect it to return three.
So my question is, are you suppose to use eval(form.vars.field)
instead, or have I indeed hit on a bug, or am I just confused
as to the usage? Any help is appreciated.
Thanks,
Matt B.
From: mdipierro
Date: Thu, 14 May 2009 11:51:34 -0700 (PDT)
Local: Thurs, May 14 2009 2:51 pm
Subject: Re: bug in form behavior?
you may have found a bug. I will look at it asap.
Massimo
From: DenesL
Date: Thu, 14 May 2009 15:17:05 -0700 (PDT)
Local: Thurs, May 14 2009 6:17 pm
Subject: Re: bug in form behavior?
Besides the problem in the INPUT class
_validate function there is one
more problem here, note that given:
inp=INPUT(_name='field')
then inp.attributes is:
{'type': 'text', 'value' :None, "_name': 'field'}
shouldn't that be '_type' instead of 'type' ?
From: mdipierro
Date: Thu, 14 May 2009 19:40:43 -0700 (PDT)
Local: Thurs, May 14 2009 10:40 pm
Subject: Re: bug in form behavior?
Do this instead:
if form.accepts(request.vars, session):
return len(request.vars.field)
I am not sure whether this should be considered
a bug since the process of supporting multiple fields
with the same name is only supported for checkboxes.
I will look into this some more anyway.
I strongly suggest giving different names to the
three INPUT fields else you will not be able to reference
them using validators (which why this is not supported).
Massimo
From:
mgbeli...@gmail.com
Date: Thu, 14 May 2009 21:01:46 -0700 (PDT)
Local: Fri, May 15 2009 12:01 am
Subject: Re: bug in form behavior?
Thanks massimo.
At your convenience of course. I will usually give
different names, but the identical field names
helped me out in this app
https://www.lemurstrikes.com/portfolio/default/sudoku
right here.
By the way, I'll be adding my first app to the
KPAX app list here soon.
-Matt B.
From: DenesL
Date: Fri, 15 May 2009 07:20:09 -0700 (PDT)
Local: Fri, May 15 2009 10:20 am
Subject: Re: bug in form behavior?
@mgbelisle
Nice solvers.
Massimo
web2py is (correctly IMO) building request.vars.field
as a list from the three INPUTs, so for consistency
I would expect form.vars.field to be an exact copy
after accepts. There is nothing in the w3.org specs
against the use of equally named INPUT controls outside
the scope of checkboxes and radio buttons,
but you can set it as a web2py "restriction by design".
My other comment was also geared towards consistency:
inp1=INPUT(_name='field')
inp2=INPUT(_name='field',_type='text')
# as used in widgets
# inp1.attributes
# {'type':'text','value':None,'_name':'field'}
# inp2.attributes
# {'_type':'text','value':None,'_name':'field'}
I know that you internally manipulate 'type'
and '_type' and the logic has been working so far,
but it just seems inconsistent.
From: mdipierro <mdipie...@cs.depaul.edu>
Date: Fri, 15 May 2009 08:37:14 -0700 (PDT)
Local: Fri, May 15 2009 11:37 am
Subject: Re: bug in form behavior?
This is now fixed in trunk. Please check.
I hope it did not break anything else.
From: mdipierro
Date: Fri, 15 May 2009 08:37:47 -0700 (PDT)
Local: Fri, May 15 2009 11:37 am
Subject: Re: bug in form behavior?
This is fixed too. Can you please check?
Thanks
Massimo
From:
mgbeli...@gmail.com
Date: Sat, 16 May 2009 08:57:27 -0700 (PDT)
Local: Sat, May 16 2009 11:57 am
Subject: Re: bug in form behavior?
Yes, I'm downloading the trunk version now
and testing it out. I'll let you know how it goes Massimo.
From:
mgbeli...@gmail.com
Date: Sat, 16 May 2009 09:53:42 -0700 (PDT)
Local: Sat, May 16 2009 12:53 pm
Subject: Re: bug in form behavior?
Yes Massimo, it does indeed work in the trunk
and I haven't run into any bad side effects either.
Thanks for the speedy fix!
-Matt
From murray3
If I have a page with
<h3>
{ {=A(comment.title.upper(),_href=URL(r=request,f='edit_comment',args=[comment.id])) } }
</h3
if I want to put edit_comment in an iframe and change the above code
so it refreshes the iframe and passes the args=[comment.id] to the
iframe, what is the syntax?
I have a list of links and as each is clicked I want an onclick event
to dynamically update the iframe page.
any help appreciated.
Chrism
am guessing you want
<iframe src="{ {=URL(r=request,f='edit_comment',args=comment.id)) } }"></iframe>
Massimo
Thanks Massimo,
I already have the iframe code working as per your suggestion on a page.
What I would like to do is refresh the iframe when I click on a link
on the main page the iframe is embedded within.
Also the link should pass the args to the iframe so that the correct
db rows are shown.
The links are the headers for discussion threads, when one is clicked
the iframe should show the threads under that header
I have this javascript code which will refresh an iframe as I could
not find out how to do it with jQuery:
<script type="text/javascript">
function refreshiframe(el)
{
var f =document.getElementById(el)
var rsrc = f.src
f.src="about:blank"
f.src=rsrc
}
</script>
What I want to do is use this or similar to refresh but parse the args
to the iframe so it refreshes and shows correct db rows. here is html I am using:
<table>
<tr>
<td>
{ {for comment in comments:} }
<h2></h2>
<p>
<h3>
{ {=A(comment.title.upper(),_href=
URL(r=request,f='ii',args=[comment.id]),_onclick="iframerefresh(ii)") } }
</h3>
</p>
<p>
posted by { {=comment.author_alias} } on { {=comment.timestamp} }
</p>
{ {pass} }
{ {=form} }
</td>
<td>
<iframe name="ii" id="ii" scrolling=" no" height="600" width="500"
frameborder: 0" src="{ {=URL(r=request,f='edit_comment',args=[comment.id] ) } }">
</iframe>
</td>
</tr>
</table>
obviously my code -
href=URL(r=request,f='ii',args=[comment.id]),_onclick="iframerefresh(ii)"
is incorrect but not sure what synatx should be??
thanks
Chrism
mdipierro
May 2, 7:11 pm
I suggest you use jQuery.
This should work
<iframe src="...." id="myiframe"></iframe>
<a href="#null" onlick="$('#myiframe').attr('src','{ {=URL(....) } }');">click me to refresh iframe</a>
I
suppose your used to it by now. :D
This works... except for the validators... they aren't stoping me from
entering more than one student_name
from datetime import datetime, date, timenow = datetime.utcnow()
today = date.today()
db = SQLDB('sqlite://database.db')
db.define_table('student',
SQLField('student_name', 'string'),
SQLField('certificate_number', 'integer'),
SQLField('security_number', 'string'),
SQLField('grade_point_average', 'double'),
SQLField('thesis_name', 'string'),
SQLField('thesis_grade', 'integer'))
db.student.student_name.requires = IS_NOT_IN_DB(db, "student.student_name")
db.student.student_name.requires = IS_NOT_EMPTY()
# BUG!! Multiple .requires on same field!!
db.student.certificate_number.requires = IS_NOT_IN_DB(db,"student.certificate_number")
db.student.thesis_grade.requires = IS_IN_SET(range(1,6))
db.student.grade_point_average.requires = IS_FLOAT_IN_RANGE(1,5)
db.student.certificate_number.requires = IS_NOT_EMPTY()
Also, note, that you are trying to combine two FORM validators.
It can be done with requires.append()
Since the first requirement belongs on the DAL / on db checking, so the
database can raise an error, then your second requirement is _clearly_
(correct?) a form validator: you want the form to not be accepted if it is
submitted without a name...
So you mean is more clearly said like this:
db.define_table('student', SQLField('student_name', 'string', uniq=True),
Since you probably don't want student name dropdown boxes in the form,then this will work:
db.student.student_name.requires = [ IS_NOT_IN_DB(db,"student.student_name") ]
....
db.student.student_name.requires.
append
( IS_NOT_EMPTY() )
Dropdown boxes are only created for values required to be in a
definite set or a database, i.e. IS_IN_SET or IS_IN_DB validators.
If you don't want the dropdown then you put in a list.
============================================================================
|
From http://groups.google.com/group/web2py/browse_thread/thread/8d2cf76f0754c238 From: "Sebastian E. Ovide" <sebastianov...@gmail.com> Date: Wed, 6 May 2009 18:20:49 +0100 Local: Wed, May 6 2009 1:20 pm Subject: about FORM and db.update Hi All, in an action that updates a FORM , when the form accepts the new values, it updates the DB correctly, but when redraw the page it display the old values. If I press Submit again (keeping the old values in the form) it displays the new values entered previously... and so on for ever.... the work around was to add a redirect to the same controller just after the db.update. Is it the correct way to deal with custom FORMS ? Am I missing something ? thanks
def test(): form = None # find tests profile of current user query=db.tests.owner_id==auth.user.id tests = db(query).select() if len(tests)>0: #get the first found test=tests[0] form=FORM(TABLE(TR("Name:",INPUT(_type="text",_name="name",value= test.name,requires=IS_NOT_EMPTY())), TR("",INPUT(_type="submit",_value="SUBMIT")) )) if form.accepts(request.vars,session): db(query).update(name=form.vars.name) # HERE THE REDIRECT !!!!!! redirect(URL(r=request,f="test")) return dict(form=form) ---------------------------- Sebastian E. Ovide
From: mdipierro <mdipie...@cs.depaul.edu> Date: Wed, 6 May 2009 10:52:13 -0700 (PDT) Local: Wed, May 6 2009 1:52 pm Subject: Re: about FORM and db.update You can do if form.accepts(request.vars,session,keepvalues=True) You can also simplify your code def test(): tests = db(db.tests.owner_id==auth.user.id).select() if tests: form=SQLFORM(db.tests,tests[0],fields=['name']) if form.accepts(request.vars,session): # session.flash = '....' redirect(URL(r=request,f="test")) else: form=None return dict(form=form)
From: "Sebastian E. Ovide" <sebastianov...@gmail.com> Date: Wed, 6 May 2009 19:08:18 +0100 Local: Wed, May 6 2009 2:08 pm Subject: Re: [web2py:21312] Re: about FORM and db.update Thanks Massimo ! I've tried to use SQLFORM before but it displays also the ID even if I do not include it in the fields list. Furthermore I need to change some fields defaults such as TEXTAREA size etc... (in my app there are more than 1 field) ---------------------------- Sebastian E. Ovide
From: mdipierro <mdipie...@cs.depaul.edu> Date: Wed, 6 May 2009 11:41:22 -0700 (PDT) Local: Wed, May 6 2009 2:41 pm Subject: Re: about FORM and db.update
SQLFORM(...,showid=False)
|
===============================================================================
From http://groups.google.com/group/web2py/browse_thread/thread/53e5e90d5a62ca2d
From: opedge <ope...@gmail.com>
Date: Tue, 5 May 2009 13:40:19 -0700 (PDT)
Local: Tues, May 5 2009 4:40 pm
Subject: Filling form after submitting
I have multiple input fields with the same name. After submitting form
I have my input fields filled with values like ['','','',''].
Can web2py keep values for multiple input fields?
From: mdipierro <mdipie...@cs.depaul.edu>
Date: Tue, 5 May 2009 16:41:10 -0700 (PDT)
Local: Tues, May 5 2009 7:41 pm
Subject: Re: Filling form after submitting
Why do you have multiple fields with the same names? In general this
should only be done for checkboxes. Is that the case?
Massimo
From: opedge <ope...@gmail.com>
Date: Wed, 6 May 2009 02:03:06 -0700 (PDT)
Local: Wed, May 6 2009 5:03 am
Subject: Re: Filling form after submitting
My problem consists in work with graphs. I use a table of text input
fields to set the values of the paths in graph.
From: mdipierro <mdipie...@cs.depaul.edu>
Date: Wed, 6 May 2009 06:50:39 -0700 (PDT)
Local: Wed, May 6 2009 9:50 am
Subject: Re: Filling form after submitting
A graph consists of a set of vertices and a set of links.
I think you should have a table for vertices and a able for links.
Something like this:
db.define_table('vertex',SQLField('metadata'))
db.define_table('link',SQLField('source',db.vertex),SQLField
('dest',db.vertex),SQLField('metadata'))
db.link.source=IS_IN_DB(db,'db.vertex.id')
db.link.dest=IS_IN_DB(db,'db.vertex.id')
from gluon.tools import Crud
crud=Crud(globals(),db)
def build():
form1=crud.create(db.vertex,next=URL(r=request))
form2=crud.create(db.link,next=URL(r=request))
vertices=db(db.vertex.id>0).select()
links=db(db.link.id>0).select()
return dict(form1=form1,form2=form2,vertices=vertces,links=links)
Massimo
================================================================================
From http://groups.google.com/group/web2py/browse_thread/thread/e7c53c08c28ca5e7
From: murray3 <ch...@murraypost.net>
Date: Fri, 1 May 2009 16:24:53 -0700 (PDT)
Local: Fri, May 1 2009 7:24 pm
Subject: html helpers
Â
If I have a page with
<h3>
{ {=A(comment.title.upper(),_href=URL(r=request,f='edit_comment',args=[comment.id]))} }
</h3
if I want to put edit_comment in an iframe and change the above code
so it refreshes the iframe and passes the args=[comment.id] to the
iframe, what is the syntax?
I have a list of links and as each is clicked I want an onclick event
to dynamically update the iframe page.
any help appreciated.
Chrism
From: mdipierro <mdipie...@cs.depaul.edu>
Date: Sat, 2 May 2009 14:50:52 -0700 (PDT)
Local: Sat, May 2 2009 5:50 pm
Subject: Re: html helpers - Done in FLI
Â
I am guessing you want
<iframe src="{ {=URL(r=request,f='edit_comment',args=comment.id))} }"></iframe>
Massimo
From: murray3 <ch...@murraypost.net>
Date: Sat, 2 May 2009 15:14:17 -0700 (PDT)
Local: Sat, May 2 2009 6:14 pm
Subject: Re: html helpers
Thanks Massimo,
I already have the iframe code working as per your suggestion on a page.
What I would like to do is refresh the iframe when I click on a link
on the main page the iframe is embedded within.
Also the link should pass the args to the iframe so that the correct
db rows are shown.
The links are the headers for discussion threads, when one is clicked
the iframe should show the threads under that header
I have this javascript code which will refresh an iframe as I could
not find out how to do it with jQuery:
<script type="text/javascript">
function refreshiframe(el)
{
var f =document.getElementById(el)
var rsrc = f.src
f.src="about:blank"
f.src=rsrc
}
</script>
What I want to do is use this or similar to refresh but parse the args
to the iframe so it refreshes and shows correct db rows.
here is html I am using:
<table>
<tr>
<td>
{ {for comment in comments:} }
<h2></h2>
<p>
<h3>
{ {=A(comment.title.upper(),_href=URL(r=request,f='ii',args= [comment.id]),_onclick="iframerefresh(ii)")} } </h3>
</p>
<p> posted by { {=comment.author_alias} } on { {=comment.timestamp} }
</p>
{ {pass} }
{ {=form} }
</td>
<td>
<h2></h2>
<iframe name="ii" id="ii" scrolling=" no" height="600"
width="500" frameborder: 0" src="{ {=URL (r=request,f='edit_comment',args=[comment.id])} }">
</iframe>
</td>
</tr>
</table>
obviously my code - href=URL(r=request,f='ii',args= [comment.id]),_onclick="iframerefresh(ii)"
is incorrect but not sure what synatx should be??
thanks
Chrism
From: mdipierro <mdipie...@cs.depaul.edu>
Date: Sat, 2 May 2009 16:11:34 -0700 (PDT)
Local: Sat, May 2 2009 7:11 pm
Subject: Re: html helpers
I suggest you use jQuery. This should work
<iframe src="...." id="myiframe"></iframe>
<a href="#null" onlick="$('#myiframe').attr('src','{ {=URL (....)} }');">click me to refresh iframe</a>