Oh I see. The validation does at least seem to be working on the
second form. It won't let me enter values not specified in the model.
If I am to use SQLFORM factory, how do I do an update/delete?
Kenneth said:
"As I understand the only option is to use factory, if you wan t to
update you have to create a form with factory then fill all fields
with
db.t_table.f_field.value = something"
But I'm not exactly sure how to fill the fields in with that method.
Could somebody give me an example in the following context?:
def test_add_user():
form=SQLFORM.factory(db.user,db.address)
if form.accepts(request.vars):
id = db.user.insert(**db.client._filter_fields(form.vars))
form.vars.client=id
id = db.address.insert(**db.address._filter_fields(form.vars))
response.flash='Thanks for filling the form'
return dict(form=form)
On Jan 21, 4:22 pm, DenesL <[email protected]> wrote:
> Doing form.append(SQLFORM(...)) creates invalid HTML since you can not
> nest FORMs, but your browser is saving the day by ignoring the nested
> form and including its fields only.
>
> Moreover form.accepts only validates your db.user fields, and none of
> db.address (unless they happen to be named the same).
>
> You should be following what Bruno
> recommended:http://web2pyslices.com/main/slices/take_slice/102
>
> On Jan 21, 3:48 pm, Lennon <[email protected]> wrote:
>
> > I solved my problem. Since the form.vars were passing through to the
> > controller, I just simply did my own manual insert:
>
> > def test_add_user():
>
> > form = SQLFORM(db['user'])
> > form.append(SQLFORM(db['address']))
>
> > #submit data
> > if form.accepts(request.vars):
>
> > #check whether the form had errors
> > if form.errors:
> > response.flash = 'Form has errors'
> > return dict(form=form)
> > else:
>
> > db.address.insert(user_id=form.vars.id,address=form.vars.address)
> > session.flash = "User Added"
> > redirect(URL('admin', 'test_add_user'))
>
> > #return the view
> > return dict(form=form)
>
> > Now all I have to do is remove the first submit button.
>
> > On Jan 21, 2:30 pm, Lennon <[email protected]> wrote:
>
> > > I have two reasons for not wanting to use SQLFORM factory. First, I
> > > can't figure out or find how to use SQLFORM factory as an update/
> > > delete form when combining multiple table (although I'd love to see
> > > how if it can be done). Second, I will be wanting to do some really
> > > complicated forms in the future and would like to understand how to
> > > combine SQLforms.
>
> > > With the insert form code listed below, I've been able to combine the
> > > two forms on one page. They both render (with two submit buttons) and
> > > the form validation works for both of them regardless of which submit
> > > button I hit (which made me think I had successfully combined them).
>
> > > But when the form accepts the data, it only does an insert on the
> > > original form and doesn't work on the appended form.
>
> > > When I run in debug mode I see that FORM.vars correctly has captured
> > > all of the values for both forms. But FORM.table and FORM.fields both
> > > only have the values for the first SQLFORM.
>
> > > Thanks for any help you can give me.
>
> > > def test_add_user():
>
> > > form = SQLFORM(db['user'])
> > > form.insert(len(form),SQLFORM(db['address']))
>
> > > #submit data
> > > if form.accepts(request.vars):
>
> > > #check whether the form had errors
> > > if form.errors:
> > > response.flash = 'Form has errors'
> > > return dict(form=form)
> > > else:
>
> > > session.flash = "User Added"
> > > redirect(URL('admin', 'test_add_user'))
>
> > > #return the view
> > > return dict(form=form)
>
>