我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用wtforms.BooleanField()。
def renderAsFormGroup(self): hiddenFields = [] for fieldName, kwargs in self.fieldsWithoutLabel: field = getattr(self, fieldName) hiddenFields.append(field(**kwargs)) visibleFields = [] for fieldName, kwargs in self.fieldsWithLabel: field = getattr(self, fieldName) if isinstance(field, BooleanField): visibleFields.append(self._buildCheckboxField(field, **kwargs)) pass else: visibleFields.append(self._buildLabelField(field, **kwargs)) return ''.join(hiddenFields) + ''.join(visibleFields)
def build_file_select_form(schema): class FileSelectForm(Form): pass for field_name, field_schema in schema.items(): if field_schema['type'] == 'boolean': field = BooleanField() elif field_schema['type'] == 'string': if 'allowed' in field_schema: choices = [(c, c) for c in field_schema['allowed']] field = SelectField(choices=choices) else: field = StringField() elif field_schema['type'] == 'objectid': field = FileSelectField('file') else: raise ValueError('field type %s not supported' % field_schema['type']) setattr(FileSelectForm, field_name, field) return FileSelectForm
def factory(cls, data, **kwargs): for d in data: if d.key == 'default_search_quality': setattr(cls, d.key, SelectMultipleField(d.name, choices=app.config.get('AVAILABLE_QUALITIES'), default=d.value, validators=[Optional()])) elif d.type == 'list': setattr(cls, d.key, SelectMultipleField(d.name, default=d.value)) elif d.type == 'string': setattr(cls, d.key, StringField(d.name, default=d.value)) elif d.type == 'bool': setattr(cls, d.key, BooleanField(d.name, default=d.value)) elif d.type == 'float': setattr(cls, d.key, FloatField(d.name, default=d.value)) elif d.type == 'int': setattr(cls, d.key, IntegerField(d.name, default=d.value)) return cls(**kwargs)
def add_form_properties(form_class, node_type): """Add fields to a form based on the node and form schema provided. :type node_schema: dict :param node_schema: the validation schema used by Cerberus :type form_class: class :param form_class: The form class to which we append fields :type form_schema: dict :param form_schema: description of how to build the form (which fields to show and hide) """ for prop_name, schema_prop, form_prop in iter_node_properties(node_type): # Recursive call if detects a dict field_type = schema_prop['type'] if field_type == 'dict': assert prop_name == 'attachments' field = attachments.attachment_form_group_create(schema_prop) elif field_type == 'list': if prop_name == 'files': schema = schema_prop['schema']['schema'] file_select_form = build_file_select_form(schema) field = FieldList(CustomFormField(file_select_form), min_entries=1) elif 'allowed' in schema_prop['schema']: choices = [(c, c) for c in schema_prop['schema']['allowed']] field = SelectMultipleField(choices=choices) else: field = SelectMultipleField(choices=[]) elif 'allowed' in schema_prop: select = [] for option in schema_prop['allowed']: select.append((str(option), str(option))) field = SelectField(choices=select) elif field_type == 'datetime': if form_prop.get('dateonly'): field = DateField(prop_name, default=date.today()) else: field = DateTimeField(prop_name, default=datetime.now()) elif field_type == 'integer': field = IntegerField(prop_name, default=0) elif field_type == 'float': field = FloatField(prop_name, default=0.0) elif field_type == 'boolean': field = BooleanField(prop_name) elif field_type == 'objectid' and 'data_relation' in schema_prop: if schema_prop['data_relation']['resource'] == 'files': field = FileSelectField(prop_name) else: field = StringField(prop_name) elif schema_prop.get('maxlength', 0) > 64: field = TextAreaField(prop_name) else: field = StringField(prop_name) setattr(form_class, prop_name, field)