1๐
โ
I think you might be running into issues with using other values from cleaned_data
in a clean_{field}
function. switch it to a general clean
function and you may have better luck. Also, its better form, because its a clean method that requires two fieldsโ values in order for the validation to run.
def clean(self):
cleaned_data = self.cleaned_data
searchterm = cleaned_data.get('searchterm')
search_type = cleaned_data.get('search_type')
if search_type == 'regex':
try:
re.search(searchterm, 'randomdatastring') #this is just to test if the regex is valid
except re.error:
raise forms.ValidationError("Invalid regular expression.")
return cleaned_data
Source:stackexchange.com