0

I'm attempting to check the 'image' field on my form to see if it's valid. Within my view, I've got the following code:

if form.is_valid():
   #some stuff

else:
   errors = form.errors.as_data()
   for error in errors:
      if error == 'image':
         print('There was an error with the image!')

I have a custom validator that causes the form to not validate if the image that the user attempts to upload is > 5MB. When I upload an image > 5MB, I get the following error:

'EditUserProfileForm' object has no attribute 'image'

This form definitively has an 'image' field on it. It's a model form and the model also has an 'image' field. The user can successfully upload a photo if its less than 5MB, so its not anything else that's broken.

Edit: I've updated the else statement. The print statement works when there is an image error (i.e. it is over 5mb), but it doesn't seem possible to change the image field at this point in the code. I get an error message that indicates that "The UserProfile could not be changed because the data didn't validate" or "This QueryDict instance is immutable" whenever I try to change either the value on the model instance or the form value, respectively.

Changing the form value seems possible only in forms.py if the form didn't validate.

Jason Howard
  • 1,464
  • 1
  • 14
  • 43
  • 4
    Possible duplicate of [Custom form validation](https://stackoverflow.com/questions/7948750/custom-form-validation) – Brown Bear Apr 21 '18 at 21:09
  • This answer does not indicate how to establish if there has been an error on a single field. This only explains how to build custom validation for a field. I'm looking to identify if django's built in logic or my custom validator has thrown an error on this field. – Jason Howard Apr 22 '18 at 01:46

1 Answers1

0

You could try parsing the errors from validation errors you got from the form that caused it go reach the "else" branch using Form.errors.as_data().

This returns a dictionary with fields as keys and errors as values. You can loop through it to see if any of the validation errors are originating with the "image" field.

pseudoku
  • 716
  • 4
  • 11
  • So I've given this a try. I'm able to loop through the errors and when error == image, I've tried to a) change the form data to None for image. I'm told that the "This QueryDict instance is immutable". Looks like you can't modified post data in this way. I think tried to change the field value within the model. Django would let me do this either: "The UserProfile could not be changed because the data didn't validate." Seems like changing a form value if the form didn't validate needs to be done in forms.py rather than in the else section after if form.is_valid(). Thanks for the info though. – Jason Howard Apr 22 '18 at 02:02
  • Ah, sorry I misunderstood what you needed. Since you probably don't want to try to exclude image from all validation, maybe you can modify that loop to display the error value, and see why exactly it's raising an exception (something you couldn't do before because the error implied there wasn't an image field to begin with). You can then look into working with whatever issue is actually causing this exception. – pseudoku Apr 22 '18 at 13:58