我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用wtforms.fields.FormField()。
def test_populate_errors_when_formfield(self): class SubMyForm(BaseForm): subattribute1 = StringField() class MyForm(BaseForm): attribute1 = FormField(SubMyForm) errors = {'attribute1': {'subattribute1': 'suberror1'}} with app.test_request_context(): form = MyForm() form.populate_errors(errors) assert_that(form, has_properties( attribute1=has_properties(form=has_properties( subattribute1=has_properties(errors=contains('suberror1')) )) ))
def to_dict(self): result = {} for name, f in self._fields.items(): if name == 'csrf_token' or isinstance(f, SubmitField): continue elif isinstance(f, FormField): result[name] = f.form.to_dict() elif isinstance(f, FieldList): result[name] = [entry.to_dict() for entry in f.entries] elif not f.raw_data and f.default is None: continue else: default = f.default or f.data data = f.data if f.data else default result[name] = data if data != '' else None return result
def register_destination_form(type_id, type_label, form, position=-1): if (type_id, type_label) not in _destination_choices: _destination_choices.insert(position, (type_id, type_label)) if getattr(form, 'select_field', False): setattr(DestinationForm, type_id, DestinationField(destination_form=form)) else: setattr(DestinationForm, type_id, FormField(form))
def _create_dynamic_destination_form(self, destination): class DynamicForm(BaseForm): pass for key in destination[destination[self.select_field]]: setattr(DynamicForm, key, StringField()) options = dict(name=destination[self.select_field], prefix=self._prefix, translations=self.meta.get_translations(self)) field = self.meta.bind_field(self, FormField(DynamicForm), options) self._fields[destination[self.select_field]] = field self.added_dynamic_choice = (destination[self.select_field], destination[self.select_field])
def test_to_dict_with_formfield(self): class SubMyForm(BaseForm): subattribute = StringField() class MyForm(BaseForm): attribute = FormField(SubMyForm) with app.test_request_context(method='POST', data={'attribute-subattribute': 'subvalue'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute=has_entries(subattribute='subvalue')))
def test_to_dict_with_listfield_of_fieldform(self): class SubMyForm(BaseForm): subattribute = StringField() class MyForm(BaseForm): attribute = FieldList(FormField(SubMyForm)) with app.test_request_context(method='POST', data={'attribute-0-subattribute': 'subvalue'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute=contains(has_entries(subattribute='subvalue'))))
def populate_errors(self, resource): for form_name, form_value in self._fields.items(): if form_name not in resource: continue if isinstance(form_value, FormField): form_value.form.populate_errors(resource[form_name]) elif isinstance(form_value, FieldList): for index, form in enumerate(form_value.entries): form.populate_errors(resource[form_name].get(str(index), {})) else: # normally it's form.validate() that make this conversion form_value.errors = list(form_value.errors) form_value.errors.append(resource[form_name])
def conv_EmbeddedDocument(self, model, field, kwargs): kwargs = { 'validators': [], 'filters': [], 'default': field.default or field.document_type_obj, } form_class = model_form(field.document_type_obj, field_args={}) return f.FormField(form_class, **kwargs)