[Answered ]-Django Error: Ensure this value has at most 10 characters (it has 321)

1đź‘Ť

âś…

Per the documentation

Always return a value to use as the new cleaned data, even if this
method didn’t change it. The return value of this method replaces the existing value in cleaned_data, so it must be the field’s value from cleaned_data (even if this method didn’t change it) or a new cleaned value.

So for your case, you probably want to amend clean_id to the following to return the value, not all of cleaned_data:

def clean_ID(self):
  ID = self.cleaned_data.get('ID')
  if not ID.isdecimal():
    raise forms.ValidationError("ID must to contain only digits.")
  elif len(ID) != 9:
    raise forms.ValidationError("ID must to contain 9 digits.")
  # return self.cleaned_data   # <--- DON'T DO THIS
  return ID
👤AMG

Leave a comment