Here are the examples of the python api django.forms.utils.ErrorDict taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
9 Examples
3
View Complete Implementation : forms.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def full_clean(self):
"""
Cleans all of self.data and populates self._errors and
self.cleaned_data.
"""
self._errors = ErrorDict()
if not self.is_bound: # Stop further processing.
return
self.cleaned_data = {}
# If the form is permitted to be empty, and none of the form data has
# changed from the initial data, short circuit any validation.
if self.empty_permitted and not self.has_changed():
return
self._clean_fields()
self._clean_form()
self._post_clean()
3
View Complete Implementation : forms.py
Copyright MIT License
Author : hugobessa
Copyright MIT License
Author : hugobessa
def full_clean(self):
"""
Clean all of self.data and populate self._errors and self.cleaned_data.
"""
from django.forms.utils import ErrorDict
self._errors = ErrorDict()
if not self.is_bound: # Stop further processing.
return
self.cleaned_data = {}
# If the form is permitted to be empty, and none of the form data has
# changed from the initial data, short circuit any validation.
if self.empty_permitted and not self.has_changed():
return
self._clean_fields()
self._clean_tenant_specific_fields()
self._clean_form()
self._post_clean()
3
View Complete Implementation : forms.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def full_clean(self):
"""
Clean all of self.data and populate self._errors and self.cleaned_data.
"""
self._errors = ErrorDict()
if not self.is_bound: # Stop further processing.
return
self.cleaned_data = {}
# If the form is permitted to be empty, and none of the form data has
# changed from the initial data, short circuit any validation.
if self.empty_permitted and not self.has_changed():
return
self._clean_fields()
self._clean_form()
self._post_clean()
0
View Complete Implementation : param.py
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
def _clean_dependency(self, name=None):
if name is None:
names = self._cleaned_data.keys()
else:
names = [name]
errors = ErrorDict()
for name in names:
if name in self._dependency:
try:
field, pairs = self._dependency[name]
try:
_ = field.clean(self._cleaned_data[name])
except forms.ValidationError:
continue
for sub_name, sub_field in pairs:
_ = sub_field.clean(self._cleaned_data[sub_name]) # NOQA
except forms.ValidationError as exc:
error_dict = TextErrorDict([(sub_name, exc.messages)])
errors[name] = [error_dict]
del self._cleaned_data[name]
if errors:
raise forms.ValidationError(errors)
0
View Complete Implementation : shop.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
def full_clean(self):
super(AddressForm, self).full_clean()
if not self.is_primary:
if self.is_bound and self['use_primary_address'].value():
self._errors = ErrorDict()
0
View Complete Implementation : forms.py
Copyright MIT License
Author : hugobessa
Copyright MIT License
Author : hugobessa
def get_tenant_specific_table_row_form_clast(table_name):
table_id = TenantSpecificTable.objects.get(name=table_name).id
tenant_specific_fields_definitions = TenantSpecificFieldDefinition.objects.filter(
table_content_type=ContentType.objects.get_for_model(TenantSpecificTable),
table_id=table_id
)
tenant_specific_fields_names = list(tenant_specific_fields_definitions.values_list('name', flat=True))
clast TenantSpecificTableRowForm(forms.ModelForm):
clast Meta:
model = TenantSpecificTableRow
fields = ['id']
form_tenant_specific_field_mapping = {
'integer': forms.IntegerField,
'char': forms.CharField,
'text': forms.CharField,
'float': forms.FloatField,
'datetime': forms.DateTimeField,
'date': forms.DateField,
}
def __init__(self, *args, **kwargs):
super(TenantSpecificTableRowForm, self).__init__(*args, **kwargs)
for definition in tenant_specific_fields_definitions:
if not self.fields.get(definition.name, False):
field_kwargs = {}
if definition.is_required:
field_kwargs.update({
'required': True,
'allow_null': True
})
if definition.default_value is not None:
field_kwargs.update(
{'initial': definition.default_value})
self.fields[definition.name] = \
self.form_tenant_specific_field_mapping[definition.data_type](
**field_kwargs)
def full_clean(self):
"""
Clean all of self.data and populate self._errors and self.cleaned_data.
"""
from django.forms.utils import ErrorDict
self._errors = ErrorDict()
if not self.is_bound: # Stop further processing.
return
self.cleaned_data = {'table_id': table_id}
# If the form is permitted to be empty, and none of the form data has
# changed from the initial data, short circuit any validation.
if self.empty_permitted and not self.has_changed():
return
self._clean_fields()
self._clean_tenant_specific_fields()
self._clean_form()
self._post_clean()
def _clean_tenant_specific_fields(self):
for name, field in self.fields.items():
if name in tenant_specific_fields_names:
definition = tenant_specific_fields_definitions.get(name=name)
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
value = field.clean(value)
validators = []
for validator_instance in definition.validators.all():
validator_function = import_from_string(validator_instance.module_path)
validators.append(validator_function)
validate_method = compose_list(validators)
self.cleaned_data[name] = validate_method(value)
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
except ValidationError as e:
self.add_error(name, e)
def save(self, *args, **kwargs):
self.instance.table_id = table_id
new_instance = super(TenantSpecificTableRowForm, self).save(*args, **kwargs)
return get_custom_table_manager(table_name).get(id=new_instance.id)
def _post_clean(self):
super(TenantSpecificTableRowForm, self)._post_clean()
for name, value in [(k, v) for k, v in self.cleaned_data.items() if k in tenant_specific_fields_names]:
setattr(self.instance, name, value)
return TenantSpecificTableRowForm
0
View Complete Implementation : test_utils.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_validation_error(self):
###################
# ValidationError #
###################
# Can take a string.
self.astertHTMLEqual(
str(ErrorList(ValidationError("There was an error.").messages)),
'<ul clast="errorlist"><li>There was an error.</li></ul>'
)
# Can take a unicode string.
self.astertHTMLEqual(
str(ErrorList(ValidationError("Not \u03C0.").messages)),
'<ul clast="errorlist"><li>Not π.</li></ul>'
)
# Can take a lazy string.
self.astertHTMLEqual(
str(ErrorList(ValidationError(gettext_lazy("Error.")).messages)),
'<ul clast="errorlist"><li>Error.</li></ul>'
)
# Can take a list.
self.astertHTMLEqual(
str(ErrorList(ValidationError(["Error one.", "Error two."]).messages)),
'<ul clast="errorlist"><li>Error one.</li><li>Error two.</li></ul>'
)
# Can take a dict.
self.astertHTMLEqual(
str(ErrorList(sorted(ValidationError({'error_1': "1. Error one.", 'error_2': "2. Error two."}).messages))),
'<ul clast="errorlist"><li>1. Error one.</li><li>2. Error two.</li></ul>'
)
# Can take a mixture in a list.
self.astertHTMLEqual(
str(ErrorList(sorted(ValidationError([
"1. First error.",
"2. Not \u03C0.",
gettext_lazy("3. Error."),
{
'error_1': "4. First dict error.",
'error_2': "5. Second dict error.",
},
]).messages))),
'<ul clast="errorlist">'
'<li>1. First error.</li>'
'<li>2. Not π.</li>'
'<li>3. Error.</li>'
'<li>4. First dict error.</li>'
'<li>5. Second dict error.</li>'
'</ul>'
)
clast VeryBadError:
def __str__(self):
return "A very bad error."
# Can take a non-string.
self.astertHTMLEqual(
str(ErrorList(ValidationError(VeryBadError()).messages)),
'<ul clast="errorlist"><li>A very bad error.</li></ul>'
)
# Escapes non-safe input but not input marked safe.
example = 'Example of link: <a href="http://www.example.com/">example</a>'
self.astertHTMLEqual(
str(ErrorList([example])),
'<ul clast="errorlist"><li>Example of link: '
'<a href="http://www.example.com/">example</a></li></ul>'
)
self.astertHTMLEqual(
str(ErrorList([mark_safe(example)])),
'<ul clast="errorlist"><li>Example of link: '
'<a href="http://www.example.com/">example</a></li></ul>'
)
self.astertHTMLEqual(
str(ErrorDict({'name': example})),
'<ul clast="errorlist"><li>nameExample of link: '
'<a href="http://www.example.com/">example</a></li></ul>'
)
self.astertHTMLEqual(
str(ErrorDict({'name': mark_safe(example)})),
'<ul clast="errorlist"><li>nameExample of link: '
'<a href="http://www.example.com/">example</a></li></ul>'
)
0
View Complete Implementation : test_utils.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_error_dict_copy(self):
e = ErrorDict()
e['__all__'] = ErrorList([
ValidationError(
message='message %(i)s',
params={'i': 1},
),
ValidationError(
message='message %(i)s',
params={'i': 2},
),
])
e_copy = copy.copy(e)
self.astertEqual(e, e_copy)
self.astertEqual(e.as_data(), e_copy.as_data())
e_deepcopy = copy.deepcopy(e)
self.astertEqual(e, e_deepcopy)
self.astertEqual(e.as_data(), e_copy.as_data())
0
View Complete Implementation : test_utils.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_error_dict_html_safe(self):
e = ErrorDict()
e['username'] = 'Invalid username.'
self.astertTrue(hasattr(ErrorDict, '__html__'))
self.astertEqual(str(e), e.__html__())