prevent "None" showing up in text inputs

This commit is contained in:
Jakob Ketterl 2022-06-09 17:24:53 +02:00
parent eccbdc1655
commit cec4e326c8
2 changed files with 24 additions and 2 deletions

View File

@ -1,7 +1,7 @@
from abc import ABC from abc import ABC
from owrx.modes import Modes from owrx.modes import Modes
from owrx.form.input.validator import Validator from owrx.form.input.validator import Validator
from owrx.form.input.converter import Converter, NullConverter, IntConverter, FloatConverter, EnumConverter from owrx.form.input.converter import Converter, NullConverter, IntConverter, FloatConverter, EnumConverter, TextConverter
from enum import Enum from enum import Enum
@ -106,6 +106,9 @@ class TextInput(Input):
props["type"] = "text" props["type"] = "text"
return props return props
def defaultConverter(self):
return TextConverter()
class NumberInput(Input): class NumberInput(Input):
def __init__(self, id, label, infotext=None, append="", converter: Converter = None, validator: Validator = None): def __init__(self, id, label, infotext=None, append="", converter: Converter = None, validator: Validator = None):

View File

@ -14,6 +14,10 @@ class Converter(ABC):
class NullConverter(Converter): class NullConverter(Converter):
"""
The default converter class
Does not change the value in any way, just passes them through
"""
def convert_to_form(self, value): def convert_to_form(self, value):
return value return value
@ -21,10 +25,25 @@ class NullConverter(Converter):
return value return value
class TextConverter(Converter):
"""
Converter class for text inputs
Does nothing more than to prevent the special python value "None" from appearing in the form
The string "None" should pass
"""
def convert_to_form(self, value):
if value is None:
return ""
return value
def convert_from_form(self, value):
return value
class OptionalConverter(Converter): class OptionalConverter(Converter):
""" """
Transforms a special form value to None Transforms a special form value to None
The default is look for an empty string, but this can be used to adopt to other types. The default is to look for an empty string, but this can be used to adopt to other types.
If the default is not found, the actual value is passed to the sub_converter for further transformation. If the default is not found, the actual value is passed to the sub_converter for further transformation.
useful for optional fields since None is not stored in the configuration useful for optional fields since None is not stored in the configuration
""" """