def validate_required_if_set(other_field, **kwargs):
 def _validator(form, field):
  other_field_value = getattr(form, other_field).data
  if other_field_value:
   if ((field.data is None) or (isinstance(field.data, (str, unicode)) and (not field.data.strip())) or (isinstance(field.data, FileStorage) and (not field.data.filename.strip()))):
    raise validators.ValidationError(('This field is required if %s is set.' % other_field))
  else:
   field.errors[:] = []
   raise validators.StopValidation()
 return _validator

def validate_required_if_set ( other_field , ** kwargs ) : def _validator ( form , field ) : other_field_value = getattr ( form , other_field ) . data if other_field_value : if ( ( field . data is None ) or ( isinstance ( field . data , ( str , unicode ) ) and ( not field . data . strip ( ) ) ) or ( isinstance ( field . data , FileStorage ) and ( not field . data . filename . strip ( ) ) ) ) : raise validators . ValidationError ( ( 'This field is required if %s is set.' % other_field ) ) else : field . errors [ : ] = [ ] raise validators . StopValidation ( ) return _validator
used as a validator within a wtforms .