from wtforms.validators import Optional


class OptionalIf(Optional):
    # makes a field optional if some other data is supplied
    def __init__(self, deciding_field, *args, **kwargs):
        self.deciding_field = deciding_field
        super(OptionalIf, self).__init__(*args, **kwargs)

    def __call__(self, form, field):
        deciding_field = form._fields.get(self.deciding_field)
        if deciding_field is None:
            raise Exception('no field named "{}" in form'.format(
                self.deciding_field))
        if bool(deciding_field.data):
            super(OptionalIf, self).__call__(form, field)

from wtforms.validators import Optional


class OptionalIf(Optional):
    # makes a field optional if some other data is supplied or is not supplied
    def __init__(self, deciding_field, invert=False, *args, **kwargs):
        self.deciding_field = deciding_field
        self.invert = invert
        super(OptionalIf, self).__init__(*args, **kwargs)

    def __call__(self, form, field):
        deciding_field = form._fields.get(self.deciding_field)
        if deciding_field is None:
            raise Exception('no field named "{}" in form'.format(
                self.deciding_field))
        if (bool(deciding_field.data) and deciding_field.data != 'None')\
                ^ self.invert:
            super(OptionalIf, self).__call__(form, field)
Add option to invert `OptionalIf` validator
Add option to invert `OptionalIf` validator


  from wtforms.validators import Optional
  
  
  class OptionalIf(Optional):
-     # makes a field optional if some other data is supplied
+     # makes a field optional if some other data is supplied or is not supplied
-     def __init__(self, deciding_field, *args, **kwargs):
+     def __init__(self, deciding_field, invert=False, *args, **kwargs):
          self.deciding_field = deciding_field
+         self.invert = invert
          super(OptionalIf, self).__init__(*args, **kwargs)
  
      def __call__(self, form, field):
          deciding_field = form._fields.get(self.deciding_field)
          if deciding_field is None:
              raise Exception('no field named "{}" in form'.format(
                  self.deciding_field))
-         if bool(deciding_field.data):
+         if (bool(deciding_field.data) and deciding_field.data != 'None')\
+                 ^ self.invert:
              super(OptionalIf, self).__call__(form, field)
- 
Add option to invert `OptionalIf` validator
## Code Before:
from wtforms.validators import Optional


class OptionalIf(Optional):
    # makes a field optional if some other data is supplied
    def __init__(self, deciding_field, *args, **kwargs):
        self.deciding_field = deciding_field
        super(OptionalIf, self).__init__(*args, **kwargs)

    def __call__(self, form, field):
        deciding_field = form._fields.get(self.deciding_field)
        if deciding_field is None:
            raise Exception('no field named "{}" in form'.format(
                self.deciding_field))
        if bool(deciding_field.data):
            super(OptionalIf, self).__call__(form, field)

## Instruction:
Add option to invert `OptionalIf` validator
## Code After:
from wtforms.validators import Optional


class OptionalIf(Optional):
    # makes a field optional if some other data is supplied or is not supplied
    def __init__(self, deciding_field, invert=False, *args, **kwargs):
        self.deciding_field = deciding_field
        self.invert = invert
        super(OptionalIf, self).__init__(*args, **kwargs)

    def __call__(self, form, field):
        deciding_field = form._fields.get(self.deciding_field)
        if deciding_field is None:
            raise Exception('no field named "{}" in form'.format(
                self.deciding_field))
        if (bool(deciding_field.data) and deciding_field.data != 'None')\
                ^ self.invert:
            super(OptionalIf, self).__call__(form, field)