> I have a table with 2 fields:
> db.define_table("table1",
> Field("field1", "string"),
> Field("field2", "string"))
>
> field1 is required so there should be two options:
> 1. the user enters something in field1 and field2
> 2. the user enters something in field1 and leaves field2 blank
>
models/db.py
db.define_table("table1",
Field("field1", requires = IS_NOT_EMPTY()),
Field("field2")
)
> In the second case i want to use a function that takes the value from
> field1 splits the first word and then stores it in field2.
> something like:
> def function():
> field1_value="word1 word2 word3"
> valuesplit= field1_value.split(" ")[:1]
> joinvalue= " ".join(valuesplit)
> return joinvalue
assuming you have models/db.py from the first example
contoller/default.py:
from gluon.sqlhtml import form_factory
def function():
form = form_factory(Field("field12", requires = IS_NOT_EMPTY()))
if form.accepts(request.vars, session):
word_list = form.vars.field12.split()
if len(word_list)>2:
db.table1[0] = dict(field1 = word_list[0], field2 = "
".join(word_list[1:]))
response.flash = "success"
else: response.flash = "not enough words"
return dict(form = form)
might have some typos in there but I guess you get the idea
--
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en.