Hi
I'm trying to put in several forms in a single view function. Is it
possible to find out which form has been submitted, and skip validation
and processing for all other forms? This code below validates all three
forms, so I'm getting validation messages on every form.
I dont mind to add separate view functions to process the form POSTs
separately, but the forms still need to be displayed on a single page,
preferably with the validation messages (the POST handlers need to pass
the validation data to the function that presents the forms).
Note that I do the redirect on successful validation to make sure the user
wont resubmit the form. I also would like to do the redirects when the
validation fails, but I want to keep the user input and the validation
messages. Is it possible to save this data across requests without much
pain?
form1 = FromOne()
if form1.validate_on_submit():
...
return(flask.redirect(...
form2 = FromTwo()
if form2.validate_on_submit():
...
return(flask.redirect(...
form3 = FromThree()
if form3.validate_on_submit():
...
return(flask.redirect(...
return flask.render_template('template.html', **{
'form1': form1,
'form2': form2,
'form3': form3,
} )
Thanks
Anton
What you can do is use the prefix argument for forms. Example: form1 = FormA(prefix="form1") form2 = FormB(prefix="form2") form3 = FormC(prefix="form3") Then, add a hidden field (or just check a submit field): if form1.validate_on_submit() and form1.submit.data: .... The prefix arg will ensure that each form has unique field names - so form1's submit will be called "form1_submit". On 12 September 2010 13:03, Anton Khodakivskiy <akhodakivskiy@gmail.com> wrote: > Hi > > I'm trying to put in several forms in a single view function. Is it possible to find out which form has been submitted, and skip validation and processing for all other forms? This code below validates all three forms, so I'm getting validation messages on every form. > > I dont mind to add separate view functions to process the form POSTs separately, but the forms still need to be displayed on a single page, preferably with the validation messages (the POST handlers need to pass the validation data to the function that presents the forms). > > Note that I do the redirect on successful validation to make sure the user wont resubmit the form. I also would like to do the redirects when the validation fails, but I want to keep the user input and the validation messages. Is it possible to save this data across requests without much pain? > > form1 = FromOne() > if form1.validate_on_submit(): > ... > return(flask.redirect(... > > form2 = FromTwo() > if form2.validate_on_submit(): > ... > return(flask.redirect(... > > form3 = FromThree() > if form3.validate_on_submit(): > ... > return(flask.redirect(... > > return flask.render_template('template.html', **{ > 'form1': form1, > 'form2': form2, > 'form3': form3, > } ) > > > Thanks > Anton >
Thanks for the quick response. Maybe some logic should be added the the is_submitted() method on the Form class? Right now it just checks that the method is POST/PUT, but it does not check that this particular form has been submitted. Anton On Sep 12, 2010, at 3:09 PM, Dan Jacob wrote: > What you can do is use the prefix argument for forms. > > Example: > > form1 = FormA(prefix="form1") > form2 = FormB(prefix="form2") > form3 = FormC(prefix="form3") > > Then, add a hidden field (or just check a submit field): > > if form1.validate_on_submit() and form1.submit.data: > .... > > The prefix arg will ensure that each form has unique field names - so > form1's submit will be called "form1_submit". > > On 12 September 2010 13:03, Anton Khodakivskiy <akhodakivskiy@gmail.com> wrote: >> Hi >> >> I'm trying to put in several forms in a single view function. Is it possible to find out which form has been submitted, and skip validation and processing for all other forms? This code below validates all three forms, so I'm getting validation messages on every form. >> >> I dont mind to add separate view functions to process the form POSTs separately, but the forms still need to be displayed on a single page, preferably with the validation messages (the POST handlers need to pass the validation data to the function that presents the forms). >> >> Note that I do the redirect on successful validation to make sure the user wont resubmit the form. I also would like to do the redirects when the validation fails, but I want to keep the user input and the validation messages. Is it possible to save this data across requests without much pain? >> >> form1 = FromOne() >> if form1.validate_on_submit(): >> ... >> return(flask.redirect(... >> >> form2 = FromTwo() >> if form2.validate_on_submit(): >> ... >> return(flask.redirect(... >> >> form3 = FromThree() >> if form3.validate_on_submit(): >> ... >> return(flask.redirect(... >> >> return flask.render_template('template.html', **{ >> 'form1': form1, >> 'form2': form2, >> 'form3': form3, >> } ) >> >> >> Thanks >> Anton >>
The problem there is how do you know which form is being submitted ? Beyond checking the HTTP METHOD you can't really know. You are free of course to override is_submitted() for your own form classes though to implement specific logic, for example checking for a specific field. On 12 September 2010 13:31, Anton Khodakivskiy <akhodakivskiy@gmail.com> wrote: > Thanks for the quick response. > > Maybe some logic should be added the the is_submitted() method on the Form class? Right now it just checks that the method is POST/PUT, but it does not check that this particular form has been submitted. > > Anton > > On Sep 12, 2010, at 3:09 PM, Dan Jacob wrote: > >> What you can do is use the prefix argument for forms. >> >> Example: >> >> form1 = FormA(prefix="form1") >> form2 = FormB(prefix="form2") >> form3 = FormC(prefix="form3") >> >> Then, add a hidden field (or just check a submit field): >> >> if form1.validate_on_submit() and form1.submit.data: >> .... >> >> The prefix arg will ensure that each form has unique field names - so >> form1's submit will be called "form1_submit". >> >> On 12 September 2010 13:03, Anton Khodakivskiy <akhodakivskiy@gmail.com> wrote: >>> Hi >>> >>> I'm trying to put in several forms in a single view function. Is it possible to find out which form has been submitted, and skip validation and processing for all other forms? This code below validates all three forms, so I'm getting validation messages on every form. >>> >>> I dont mind to add separate view functions to process the form POSTs separately, but the forms still need to be displayed on a single page, preferably with the validation messages (the POST handlers need to pass the validation data to the function that presents the forms). >>> >>> Note that I do the redirect on successful validation to make sure the user wont resubmit the form. I also would like to do the redirects when the validation fails, but I want to keep the user input and the validation messages. Is it possible to save this data across requests without much pain? >>> >>> form1 = FromOne() >>> if form1.validate_on_submit(): >>> ... >>> return(flask.redirect(... >>> >>> form2 = FromTwo() >>> if form2.validate_on_submit(): >>> ... >>> return(flask.redirect(... >>> >>> form3 = FromThree() >>> if form3.validate_on_submit(): >>> ... >>> return(flask.redirect(... >>> >>> return flask.render_template('template.html', **{ >>> 'form1': form1, >>> 'form2': form2, >>> 'form3': form3, >>> } ) >>> >>> >>> Thanks >>> Anton >>> > >