mercredi 23 novembre 2016

Python WTForms DataRequired only if a boolean is True

I have a form where a user can check which kind of room he wants to submit. He can choose between 4 rooms.

If he chooses a room, he checks the checkbox which makes it true and if it is true he MUST fill in all values which belong to this room. In the Image the first 2 must be filled in all and the other two should be ignored.

enter image description here

If he submits the form my code will add only the checked true rooms to the database and ignore the others.

My Problem is that it tells me either to fill all data (even if it is not checked) here I use DataRequired() or it doesnt want any data here I use Optional()

Example - Here a room is added if the checkbox is true:

if form.zimmer_doppelzimmer_bool.data == True:
    new_zimmer_doppel = Doppelzimmer_Base(art = "doppelzimmer", anzahl_zimmer = form.anzahl_zimmer_doppelzimmer.data, personen = form.personen_doppelzimmer.data, 
                                                        preis = form.preis_doppelzimmer.data, zimmer_id = new_zimmer.id)
    db_session.add(new_zimmer_doppel)

As you see if the checkbox would be not true nothing should happen.

Here is the WTForm:

anzahl_zimmer_doppelzimmer = IntegerField('Anzahl', validators=[Optional()])
personen_doppelzimmer = IntegerField('Personen', validators=[Optional()])
preis_doppelzimmer = IntegerField('Preis', validators=[Optional()])

If I use Optional() it never tells me that I need to add data, even if the checkbox is checked. If I would use DataRequired() it asks me for values for rooms which are unchecked.

So I need:

I need a way to make the InputFields required if the checkbox is checked and optional of the checkbox is unchecked. I tried already a few solutions which all not worked:

What I tried:

I found here on SO a RequiredIf method which sadly didnt worked because it check if the field is there and not None, but a boolean Field is never None:

class RequiredIf(DataRequired):
# a validator which makes a field required if
# another field is set and has a truthy value

def __init__(self, other_field_name, *args, **kwargs):
    self.other_field_name = other_field_name
    super(RequiredIf, self).__init__(*args, **kwargs)

def __call__(self, form, field):
    other_field = form._fields.get(self.other_field_name)
    if other_field is None:
        raise Exception('no field named "%s" in form' % self.other_field_name)
    if bool(other_field.data):
        super(RequiredIf, self).__call__(form, field)

I tried to create pairs of each InputField in the forms.py where I was checking whether the bool is true and load then the DataRequired() field and else load the Optional() fields but in the forms.py the bool has no value yet:

if zimmer_einzelzimmer_bool.data == True:
    anzahl_zimmer = IntegerField('Anzahl', validators=[DataRequired()])
else:
    anzahl_zimmer = IntegerField('Anzahl', validators=[Optional()])




Aucun commentaire:

Enregistrer un commentaire