def form_example(self, ctx): form = formal.Form() # This actually installs a RequiredValidator for you. form.addField('required', formal.String(required=True)) # Exactly the same as above, only with a "manually" installed validator. form.addField('required2', formal.String(validators=[formal.RequiredValidator()])) # Check for a minimum length, if anything entered. form.addField('atLeastFiveChars', formal.String(validators=[formal.LengthValidator(min=5)])) # Check for a minimum length, if anything entered. form.addField('ipAddress', formal.String(strip=True, validators=[formal.PatternValidator(regex=IP_ADDRESS_PATTERN)])) # Check for the word 'silly' form.addField('silly', formal.String(validators=[SillyValidator()])) # Check age is between 18 and 30 form.addField('ohToBeYoungAgain', formal.Integer(validators=[formal.RangeValidator(min=18, max=30)])) form.addAction(self.submitted) return form
def HostValidator(): """ Generate a formal PatternValidator to validate a Hostname """ HOST_PATTERN = '^((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$' return formal.PatternValidator(regex=HOST_PATTERN)
def IPValidator(): """ Generates a formal PatternValidator to validate an IP address""" IP_ADDRESS_PATTERN = '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' return formal.PatternValidator(regex=IP_ADDRESS_PATTERN)
def PortValidator(): """ Generates a formal PatternValidator to validate a port is between 0 and 65535 """ PORT_PATTERN = '^(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)$' return formal.PatternValidator(regex=PORT_PATTERN)
def IPMaskValidator(): """ Generates a formal PatternValidator to validate a fully qualified CIDR IP address """ IP_ADDRESS_PATTERN = '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}$' return formal.PatternValidator(regex=IP_ADDRESS_PATTERN)