From 95135c09b7690e735a264a677c6ea2ac504ae6b8 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Fri, 17 Mar 2023 11:40:15 +0100 Subject: [PATCH 1/2] Modified form validation --- django_unicorn/components/unicorn_view.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/django_unicorn/components/unicorn_view.py b/django_unicorn/components/unicorn_view.py index 66a0b7d2..8bda825c 100644 --- a/django_unicorn/components/unicorn_view.py +++ b/django_unicorn/components/unicorn_view.py @@ -531,24 +531,15 @@ def validate(self, model_names: List = None) -> Dict: # that is valid. Validating the valid one should not show an error for # the invalid one. Only after the invalid field is updated, should the # error show up and persist, even after updating the valid form. - if self.errors: - keys_to_remove = [] - for key, value in self.errors.items(): - if key in form_errors: - self.errors[key] = value + self.errors = dict() + if form_errors: + for key in form_errors: + if key != "__all__": + self.errors[key] = form_errors[key] else: - keys_to_remove.append(key) + self.errors['all'] = form_errors[key] - for key in keys_to_remove: - self.errors.pop(key) - - if model_names is not None: - for key, value in form_errors.items(): - if key in model_names: - self.errors[key] = value - else: - self.errors.update(form_errors) return self.errors From 5c7a4fd5535919eb798d3751a7d4145165b11752 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Mon, 27 Mar 2023 15:12:21 +0200 Subject: [PATCH 2/2] added validate_all field to unicorn view --- django_unicorn/components/unicorn_view.py | 28 +++++---- example/project/settings.py | 2 +- example/unicorn/components/validation_all.py | 25 ++++++++ .../templates/unicorn/validation-all.html | 63 +++++++++++++++++++ example/www/templates/www/base.html | 1 + example/www/templates/www/validation_all.html | 10 +++ 6 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 example/unicorn/components/validation_all.py create mode 100644 example/unicorn/templates/unicorn/validation-all.html create mode 100644 example/www/templates/www/validation_all.html diff --git a/django_unicorn/components/unicorn_view.py b/django_unicorn/components/unicorn_view.py index 8bda825c..8f3438c7 100644 --- a/django_unicorn/components/unicorn_view.py +++ b/django_unicorn/components/unicorn_view.py @@ -28,6 +28,7 @@ from ..utils import cache_full_tree, is_non_string_sequence, restore_from_cache from .fields import UnicornField from .unicorn_template_response import UnicornTemplateResponse +from django.core.exceptions import ValidationError try: @@ -525,22 +526,27 @@ def validate(self, model_names: List = None) -> Dict: if form: form_errors = form.errors.get_json_data(escape_html=True) - - # This code is confusing, but handles this use-case: - # the component has two models, one that starts with an error and one - # that is valid. Validating the valid one should not show an error for - # the invalid one. Only after the invalid field is updated, should the - # error show up and persist, even after updating the valid form. - + existing_errors = self.errors self.errors = dict() - if form_errors: + + if form.errors: + validate_all_fields = False + if hasattr(self, "validate_all"): + validate_all_fields= self.validate_all + for key in form_errors: - if key != "__all__": + if key == "__all__": + self.errors['all'] = form_errors[key] + + if validate_all_fields: self.errors[key] = form_errors[key] else: - self.errors['all'] = form_errors[key] - + if key in existing_errors: + self.errors[key] = form_errors[key] + if key in model_names: + self.errors[key] = form_errors[key] + return self.errors @timed diff --git a/example/project/settings.py b/example/project/settings.py index b1c4a787..94ddfeda 100644 --- a/example/project/settings.py +++ b/example/project/settings.py @@ -4,7 +4,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = "p6b$i%36e_xg%*ok+55@uc(h9)#g+2fj#p%7g#-@y8s6+10q#7" DEBUG = True -ALLOWED_HOSTS = ["localhost"] +ALLOWED_HOSTS = ["*"] INSTALLED_APPS = [ diff --git a/example/unicorn/components/validation_all.py b/example/unicorn/components/validation_all.py new file mode 100644 index 00000000..0525827a --- /dev/null +++ b/example/unicorn/components/validation_all.py @@ -0,0 +1,25 @@ +from datetime import datetime + +from django_unicorn.components import UnicornView + +from ..forms import ValidationForm + + +class ValidationAllView(UnicornView): + form_class = ValidationForm + validate_all = True + + text = "hello" + number = "" + date_time = datetime(2020, 9, 13, 17, 45, 14) + email = "" + + def set_text_no_validation(self): + self.text = "no validation" + + def set_text_with_validation(self): + self.text = "validation" + self.validate() + + def set_number(self, number): + self.number = number diff --git a/example/unicorn/templates/unicorn/validation-all.html b/example/unicorn/templates/unicorn/validation-all.html new file mode 100644 index 00000000..bf03c707 --- /dev/null +++ b/example/unicorn/templates/unicorn/validation-all.html @@ -0,0 +1,63 @@ +{% load unicorn %} + +
+ + +
+ {% unicorn_errors %} + +
+ + + {% verbatim %}{{ text }}{% endverbatim %}: {{ text }} +
+ +
+ + + {% verbatim %}{{ number }}{% endverbatim %}: {{ number }}
+ +
+ +
+ + + {{ unicorn.errors.date_time.0.message }} + +

+ {% verbatim %}{{ date_time }}{% endverbatim %}: {{ date_time }} +

+ +

+ {% verbatim %}{{ date_time|date:"s" }}{% endverbatim %}: {{ date_time|date:"s" }} +

+
+ +
+ + + {% verbatim %}{{ email }}{% endverbatim %}: {{ email }} +
+ +
+ set_text_no_validation() + + + + + + +
+
+
diff --git a/example/www/templates/www/base.html b/example/www/templates/www/base.html index e5b8ed67..9030b381 100644 --- a/example/www/templates/www/base.html +++ b/example/www/templates/www/base.html @@ -37,6 +37,7 @@

django-unicorn

  • Other inputs
  • Objects
  • Validation
  • +
  • Validation All
  • Polling
  • Models
  • Nested components (table)
  • diff --git a/example/www/templates/www/validation_all.html b/example/www/templates/www/validation_all.html new file mode 100644 index 00000000..f3c3fc6b --- /dev/null +++ b/example/www/templates/www/validation_all.html @@ -0,0 +1,10 @@ +{% extends "www/base.html" %} +{% load static unicorn %} + +{% block content %} + +

    Using Django forms for validation on all fields

    + +{% unicorn 'validation-all' hello="hello" %} + +{% endblock content %} \ No newline at end of file