From d4e61d992b90ba40cbb6e684c8152e1abc8ec7fb Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Tue, 22 Feb 2022 12:04:26 +0100 Subject: [PATCH] Raise error if trying to save when validation has failed --- ereuse_devicehub/inventory/forms.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 02cba588..41120358 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -732,7 +732,7 @@ class TradeForm(NewActionForm): email_to = self.user_to.data if not self.confirm.data and not self.code.data: - self.code.errors = ["If you don't want confirm, you need a code"] + self.code.errors = ["If you don't want to confirm, you need a code"] is_valid = False if ( @@ -757,9 +757,15 @@ class TradeForm(NewActionForm): self.db_user_to = user_to self.db_user_from = user_from + self.has_errors = not is_valid return is_valid def save(self, commit=True): + if self.has_errors: + raise ValueError( + "The %s could not be saved because the data didn't validate." + % (self.instance._meta.object_name) + ) if not self.confirm.data: self.create_phantom_account() self.prepare_instance() @@ -794,11 +800,6 @@ class TradeForm(NewActionForm): The same if exist to but not from """ - # Checks - if self.code.data: - msg = "If you don't want confirm, you need a code" - return ValidationError(msg) - user_from = self.user_from.data user_to = self.user_to.data code = self.code.data