Python wtforms.validators.Optional() Examples

The following are 5 code examples of wtforms.validators.Optional(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module wtforms.validators , or try the search function .
Example #1
Source File: reviewview.py    From radremedy with Mozilla Public License 2.0 6 votes vote down vote up
def scaffold_form(self):
        """
        Sets up the review form to ensure that the rating field
        behaves on a 1-5 scale.
        """
        form_class = super(ReviewView, self).scaffold_form()

        form_class.rating = IntegerField(
            review_column_labels['rating'],
            validators=[
                validators.Required(),
                validators.NumberRange(min=1, max=5)
            ]
        )

        form_class.staff_rating = IntegerField(
            review_column_labels['staff_rating'],
            validators=[
                validators.Optional(),
                validators.NumberRange(min=1, max=5)
            ]
        )

        form_class.intake_rating = IntegerField(
            review_column_labels['intake_rating'],
            validators=[
                validators.Optional(),
                validators.NumberRange(min=1, max=5)
            ]
        )

        return form_class 
Example #2
Source File: orm.py    From jbox with MIT License 5 votes vote down vote up
def convert(self, model, field, field_args):
        kwargs = {
            'label': field.verbose_name,
            'description': field.help_text,
            'validators': [],
            'filters': [],
            'default': field.default,
        }
        if field_args:
            kwargs.update(field_args)

        if field.blank:
            kwargs['validators'].append(validators.Optional())
        if field.max_length is not None and field.max_length > 0:
            kwargs['validators'].append(validators.Length(max=field.max_length))

        ftype = type(field).__name__
        if field.choices:
            kwargs['choices'] = field.choices
            return f.SelectField(**kwargs)
        elif ftype in self.converters:
            return self.converters[ftype](model, field, kwargs)
        else:
            converter = getattr(self, 'conv_%s' % ftype, None)
            if converter is not None:
                return converter(model, field, kwargs) 
Example #3
Source File: orm.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def convert(self, model, field, field_args):
        kwargs = {
            'label': field.verbose_name,
            'description': field.help_text,
            'validators': [],
            'filters': [],
            'default': field.default,
        }
        if field_args:
            kwargs.update(field_args)

        if field.blank:
            kwargs['validators'].append(validators.Optional())
        if field.max_length is not None and field.max_length > 0:
            kwargs['validators'].append(validators.Length(max=field.max_length))

        ftype = type(field).__name__
        if field.choices:
            kwargs['choices'] = field.choices
            return f.SelectField(**kwargs)
        elif ftype in self.converters:
            return self.converters[ftype](model, field, kwargs)
        else:
            converter = getattr(self, 'conv_%s' % ftype, None)
            if converter is not None:
                return converter(model, field, kwargs) 
Example #4
Source File: orm.py    From googleapps-message-recall with Apache License 2.0 5 votes vote down vote up
def convert(self, model, field, field_args):
        kwargs = {
            'label': field.verbose_name,
            'description': field.help_text,
            'validators': [],
            'filters': [],
            'default': field.default,
        }
        if field_args:
            kwargs.update(field_args)

        if field.blank:
            kwargs['validators'].append(validators.Optional())
        if field.max_length is not None and field.max_length > 0:
            kwargs['validators'].append(validators.Length(max=field.max_length))

        ftype = type(field).__name__
        if field.choices:
            kwargs['choices'] = field.choices
            return f.SelectField(**kwargs)
        elif ftype in self.converters:
            return self.converters[ftype](model, field, kwargs)
        else:
            converter = getattr(self, 'conv_%s' % ftype, None)
            if converter is not None:
                return converter(model, field, kwargs) 
Example #5
Source File: resourceview.py    From radremedy with Mozilla Public License 2.0 5 votes vote down vote up
def scaffold_resource_form(form_class):
    """
    Scaffolds the provided resource form class by ensuring
    location fields are optional and that nullable flag fields
    are actually handled as nullable.

    Args:
        form_class: The form class to update.
        column_labels: The column labels to use.

    Returns:
        The updated form class.
    """
    # Override the latitude/longitude fields to be optional
    form_class.latitude = DecimalField(validators=[validators.Optional()])
    form_class.longitude = DecimalField(validators=[validators.Optional()])

    # Override the nullable flag fields to actually be nullable -
    # otherwise, Flask-Admin treats them as standard Boolean fields
    # (which is bad - we want the N/A option)
    form_class.is_wpath = NullableBooleanField(
        label=resource_column_labels['is_wpath'])
    form_class.is_icath = NullableBooleanField(
        label=resource_column_labels['is_icath'])
    form_class.is_accessible = NullableBooleanField(
        label=resource_column_labels['is_accessible'])
    form_class.has_sliding_scale = NullableBooleanField(
        label=resource_column_labels['has_sliding_scale'])

    return form_class