Reputation: 235
I am passing two forms to a template, only one of these forms is compulsory while the other one is optional. Everything is fine if the user chooses to fill-out both forms, the problem comes when the user only fill-out the compulsory form and leaves the optional one, in this case, when the user submits the form, Django will prompt the user to fill the form fields of the optional form even though the user may not be interested in it.
"bankingDetailsForm" is the the optional for below while "companyProfileForm" is the compulsory one.
#userRegForm = CustomUserForm()
companyProfileForm = CompanyProfileForm()
bankingDetailsForm = BankingDetailsForm()
args = {#'userRegForm': userRegForm,
'package': packageOption,
'billing_cycle': b_cycle,
'companyProfileForm': companyProfileForm,
'bankingDetailsForm': bankingDetailsForm
}
args.update(csrf(request))
return render(request, 'user_account/subscribe.html', args)
How can I force the "bankingDetailsForm" form to be optional on submit?
Upvotes: 0
Views: 57
Reputation: 77912
Quick and dirty solution: make all your BankingDetailsForm's fields optional (required=False
), and override the form's clean()
method to only trigger full validation if one of the fields has been filled.
Upvotes: 1