Get rid of newstyle flag for Form.validate() method

we always/only use "new style" now
This commit is contained in:
Lance Edgar 2023-05-14 20:28:48 -05:00
parent a991dc0684
commit 85947878c4
17 changed files with 75 additions and 72 deletions

View file

@ -26,6 +26,7 @@ Forms Core
import json
import logging
import warnings
from collections import OrderedDict
import sqlalchemy as sa
@ -1167,49 +1168,51 @@ class Form(object):
return self.defaults[field_name]
def validate(self, *args, **kwargs):
if kwargs.pop('newstyle', False):
# yay, new behavior!
if hasattr(self, 'validated'):
del self.validated
if self.request.method != 'POST':
return False
"""
Try to validate the form.
controls = get_form_data(self.request).items()
This should work whether data was submitted as classic POST
data, or as JSON body.
# unfortunately the normal form logic (i.e. peppercorn) is
# expecting all values to be strings, whereas if our data
# came from JSON body, may have given us some Pythonic
# objects. so here we must convert them *back* to strings
# TODO: this seems like a hack, i must be missing something
# TODO: also this uses same "JSON" check as get_form_data()
if self.request.is_xhr and not self.request.POST:
controls = [[key, val] for key, val in controls]
for i in range(len(controls)):
key, value = controls[i]
if value is None:
controls[i][1] = ''
elif value is True:
controls[i][1] = 'true'
elif value is False:
controls[i][1] = 'false'
elif not isinstance(value, str):
controls[i][1] = str(value)
:returns: ``True`` if form data is valid, otherwise ``False``.
"""
if 'newstyle' in kwargs:
warnings.warn("the `newstyle` kwarg is no longer used "
"for Form.validate()",
DeprecationWarning, stacklevel=2)
dform = self.make_deform_form()
try:
self.validated = dform.validate(controls)
return True
except deform.ValidationFailure:
return False
if hasattr(self, 'validated'):
del self.validated
if self.request.method != 'POST':
return False
else: # legacy behavior
raise_error = kwargs.pop('raise_error', True)
dform = self.make_deform_form()
try:
return dform.validate(*args, **kwargs)
except deform.ValidationFailure:
if raise_error:
raise
controls = get_form_data(self.request).items()
# unfortunately the normal form logic (i.e. peppercorn) is
# expecting all values to be strings, whereas if our data
# came from JSON body, may have given us some Pythonic
# objects. so here we must convert them *back* to strings
# TODO: this seems like a hack, i must be missing something
# TODO: also this uses same "JSON" check as get_form_data()
if self.request.is_xhr and not self.request.POST:
controls = [[key, val] for key, val in controls]
for i in range(len(controls)):
key, value = controls[i]
if value is None:
controls[i][1] = ''
elif value is True:
controls[i][1] = 'true'
elif value is False:
controls[i][1] = 'false'
elif not isinstance(value, str):
controls[i][1] = str(value)
dform = self.make_deform_form()
try:
self.validated = dform.validate(controls)
return True
except deform.ValidationFailure:
return False
class FieldList(list):