I’ve a form has many inputs so I have to make some ““div”” to be hidden with a switch button like the following images
The problem is (When the inputs are hide as in the last image my form is invalid and fields error say <ul class="errorlist"><li>Enter a number.</li></ul>
)
PRODUCT-FORM FIELDS: order_limit_plus <ul class="errorlist"><li>Enter a number.</li></ul>
PRODUCT-FORM FIELDS: order_unit_plus
PRODUCT-FORM FIELDS: order_limit_minus <ul class="errorlist"><li>Enter a number.</li></ul>
PRODUCT-FORM FIELDS: order_unit_minus
PRODUCT-FORM FIELDS: expiry
PRODUCT-FORM FIELDS: expire_period <ul class="errorlist"><li>Enter a whole number.</li></ul>
PRODUCT-FORM FIELDS: expire_ref <ul class="errorlist"><li>Select a valid choice. undefined is not one of the available choices.</li></ul>
PRODUCT-FORM FIELDS: expire_period_hint <ul class="errorlist"><li>Enter a whole number.</li></ul>
PRODUCT-FORM FIELDS: hint_ref <ul class="errorlist"><li>Select a valid choice. undefined is not one of the available choices.</li></ul>
But when the fields are not hide as the first image (everything works fine)
The problem is when the (div that hold the inputs is Hidden)
I tried to give them (blank=True, null=True,) in the model but the error appears when they are hidden
my model fields
order_limit_plus = models.DecimalField(
decimal_places=2, max_digits=10,
default=0.00,
blank=True, null=True,
verbose_name=_("Order Limit"),
)
order_unit_plus = models.CharField(
max_length=20, default="",
blank=True, null=True,
)
order_limit_minus = models.DecimalField(
decimal_places=2, max_digits=10,
default=0.00,
blank=True, null=True,
verbose_name=_("Order Limit"))
order_unit_minus = models.CharField(
max_length=20, default="",
blank=True, null=True,
)
expire_period = models.DecimalField(
decimal_places=2, max_digits=10, default=0.00,
blank=True, null=True,
verbose_name=_("Expire Period")
)
expire_ref = models.CharField(
max_length=8, choices=choices.TIMES,
default=choices.HOUR,
blank=True, null=True,
)
expire_period_hint = models.DecimalField(
decimal_places=2, max_digits=10,
default=0.00,
blank=True, null=True,
)
hint_ref = models.CharField(
max_length=8, choices=choices.TIMES,
default=choices.HOUR, blank=True, null=True,
)
I tried to make them required=false
self.fields['expire_period'].required = False
self.fields['expire_period_hint'].required = False
self.fields['order_limit_plus'].required = False
self.fields['order_limit_minus'].required = False
self.fields['order_unit_plus'].required = False
self.fields['order_unit_minus'].required = False
self.fields['expire_ref'].required = False
self.fields['hint_ref'].required = False
But it is useless the error message appears because they are with a hidden div
and I tried the following
self.fields['expire_period'].widget.attrs['value'] = 0
self.fields['expire_period_hint'].widget.attrs['value'] = 0
self.fields['order_limit_plus'].widget.attrs['value'] = 0.00
self.fields['order_limit_minus'].widget.attrs['value'] = 0.00
self.fields['order_unit_plus'].widget.attrs['value'] = 0
self.fields['order_unit_minus'].widget.attrs['value'] = 0
self.fields['expire_ref'].widget.attrs['value'] = choices.TIMES[1][0]
self.fields['hint_ref'].widget.attrs['value'] = choices.TIMES[0][0]
Also I tried to pass initial values to them in the views
pro_data = {
'order_limit_minus': 0,
'order_limit_plus': 0,
'expire_period_hint':0,
'expire_period': 0,
'expire_ref': choices.TIMES[1][0],
'hint_ref': choices.TIMES[1][0],
}
form = ProductForm(
request.POST or None,
request.FILES or None,
prefix="pro",
initial=pro_data
)
Any suggestions …