MCPcopy
hub / github.com/django/django / clean

Method clean

django/forms/fields.py:1099–1155  ·  view source on GitHub ↗

Validate every value in the given list. A value is validated against the corresponding Field in self.fields. For example, if this MultiValueField was instantiated with fields=(DateField(), TimeField()), clean() would call DateField.clean(value[0]) and TimeFi

(self, value)

Source from the content-addressed store, hash-verified

1097 pass
1098
1099 def clean(self, value):
1100 """
1101 Validate every value in the given list. A value is validated against
1102 the corresponding Field in self.fields.
1103
1104 For example, if this MultiValueField was instantiated with
1105 fields=(DateField(), TimeField()), clean() would call
1106 DateField.clean(value[0]) and TimeField.clean(value[1]).
1107 """
1108 clean_data = []
1109 errors = []
1110 if self.disabled and not isinstance(value, list):
1111 value = self.widget.decompress(value)
1112 if not value or isinstance(value, (list, tuple)):
1113 if not value or not [v for v in value if v not in self.empty_values]:
1114 if self.required:
1115 raise ValidationError(
1116 self.error_messages["required"], code="required"
1117 )
1118 else:
1119 return self.compress([])
1120 else:
1121 raise ValidationError(self.error_messages["invalid"], code="invalid")
1122 for i, field in enumerate(self.fields):
1123 try:
1124 field_value = value[i]
1125 except IndexError:
1126 field_value = None
1127 if field_value in self.empty_values:
1128 if self.require_all_fields:
1129 # Raise a 'required' error if the MultiValueField is
1130 # required and any field is empty.
1131 if self.required:
1132 raise ValidationError(
1133 self.error_messages["required"], code="required"
1134 )
1135 elif field.required:
1136 # Otherwise, add an 'incomplete' error to the list of
1137 # collected errors and skip field cleaning, if a required
1138 # field is empty.
1139 if field.error_messages["incomplete"] not in errors:
1140 errors.append(field.error_messages["incomplete"])
1141 continue
1142 try:
1143 clean_data.append(field.clean(field_value))
1144 except ValidationError as e:
1145 # Collect all validation errors in a single list, which we'll
1146 # raise at the end of clean(), rather than raising a single
1147 # exception for the first error we encounter. Skip duplicates.
1148 errors.extend(m for m in e.error_list if m not in errors)
1149 if errors:
1150 raise ValidationError(errors)
1151
1152 out = self.compress(clean_data)
1153 self.validate(out)
1154 self.run_validators(out)
1155 return out
1156

Callers

nothing calls this directly

Calls 8

compressMethod · 0.95
validateMethod · 0.95
ValidationErrorClass · 0.90
extendMethod · 0.80
decompressMethod · 0.45
appendMethod · 0.45
cleanMethod · 0.45
run_validatorsMethod · 0.45

Tested by

no test coverage detected