I'm using WTForms-JSON and processing nested forms. I'd like to make an inner form optional, but if the inner form is present, I'd like its fields to be required. The problem I'm running into is that FormField
doesn't accept validators.
(Although I'm using WTForms-JSON, I believe this applies to vanilla WTForms as well.)
This code works but doesn't behave the way I want:
class InnerForm(Form):
foo_id = IntegerField("Foo ID", [Required()])
class OuterForm(Form):
inner = FormField(InnerForm)
The problem with the above code is that inner
is implicitly required. Oddly, while validate()
returns False
when inner
is omitted, errors
is empty.
This code doesn't doesn't work:
class InnerForm(Form):
foo_id = IntegerField("Foo ID", [Required()])
class OuterForm(Form):
inner = FormField(InnerForm, "Inner", [Optional()])
The latter produces this error:
TypeError: FormField does not accept any validators. Instead, define them on the enclosed form.
My question is: how can I make inner
optional, but require foo_id
if inner
is present?
The easiest way is to wrap the
FormField
in aFieldList
, withmax_entries
set to 1.FieldList
also supports validators, but sincemin_entries
is 0 by default you should not need any. The only annoyance will be that you will have to unwrap the inner form's data if it is available.