question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support validation for fields that are objects

See original GitHub issue

How do I validate an email field with Django Unicorn? Email field validation works fine with Django vanilla forms (which I was using before replacing them with unicorn ones) and with Django admin, but not at all with Unicorn. Some types of field are validated, like integer field, but not email field. I wasn’t using forms in unicorn, but since they are necessary for validation I decided to import my project Model Forms. Is that possible? And how do I validate an email field? Already tried self.validate() and if self.is_valid(), they don’t work.

I attach parts of my code to be more clear.

# companyunicorn.py

from django_unicorn.components import QuerySetType, UnicornView
from businessapp.models import Company, CompanyCategory, Contact
from django.contrib.auth.models import User, Group
from datetime import datetime,timedelta
from django.utils import timezone
from django.forms import ModelForm
from django import forms

class CompanyForm(ModelForm):
    class Meta:
        model = Company
        fields = ['__all__' ]

class CompanyunicornView(UnicornView):
    form_class = CompanyForm

    company_id = None

    company : Company = Company.objects.none()
    is_editing = False

    def __init__(self, *args, **kwargs):
        super().__init__(**kwargs)  # calling super is required
        self.company_id = kwargs.get("company_id")

    def edit(self):
        self.is_editing = True


    def save(self):
        self.company.save()
        self.is_editing = False
        self.load_data()

    def cancel(self):


        self.is_editing = False
        self.load_data()

    def mount(self):
        self.load_data()
      

    def load_data(self):
        self.company = Company.objects.get(pk=self.company_id)
        self.is_editing = False


<!-- companyunicorn.html -->

{% load static %}

<div class="col-12 col-md-7">
    {% if is_editing %}
    <div class="form-group row">
      <div class="col-sm-3 col-form-label">
        <label for="first-name">Company name</label>
      </div>
      <div class="col-sm-9">
        <input
          type="text"
          class="form-control"
          placeholder="Name"
          unicorn:model.defer="company.name"
      
        />
      </div>
      </div>

        <!-- ...so on many fields I don't post to make it more readable... -->

    <button
    unicorn:click="save"
    type="button"
    class="btn btn-primary waves-effect waves-float waves-light"
    >
      Save
    </button>
    <button
      unicorn:click="cancel"
      
      type="button"
      class="btn btn-secondary waves-effect waves-float waves-light"
    >
      Cancel
    </button>
    {% else %}
    
    <h3>{{ company.name }}</h3>
    <p class="card-text">Company name: {{ company.name }}</p>
    <!-- and more fields here, same as above... -->

    <button     unicorn:click="edit" 
           type="button"      class="btn btn-primary waves-effect waves-float waves-light"  >   Edit  </button>
  
    {% endif %}
  
   
  
  </div>

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
adamghillcommented, Jun 18, 2021

A few thoughts: In addition to supporting form_class on the component view, there could also be a {field}_form_class where “field” would refer to a field on the component. For example:

class BookForm(forms.ModelForm):
    class Meta:
        model = Book

class BookView(UnicornView):
    book_form_class = BookForm

    book: Book = None

Another option would be a form_classes on the component view which would be a dictionary that maps a component field to a form class.

class BookForm(forms.ModelForm):
    class Meta:
        model = Book

class BookView(UnicornView):
    form_classes = {
        "book": BookForm,
    }

    book: Book = None

Or maybe another option I’m not thinking of?

1reaction
adamghillcommented, Jun 18, 2021

Ah, sorry! I’m re-opening this issue. What I missed earlier was that you were traversing into the field (e.g. company.email). This is not currently possible, so I need to figure out what a good API might be to enable this. If you have any suggestions for a clean way to represent this use-case in a component, let me know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are Object Validation Rules? - Insightly Help Center
With object validation rules, administrators can write formulas to verify that certain conditions are met before records are saved. You can create validation...
Read more >
Validate object attributes in quick object creation | Jira Service ...
Select Object schema in the top right and select Configure in the dropdown. Under the General tab, select Validate object attributes in quick...
Read more >
Defining an Object-Level Validation Rule - Oracle Help Center
An object-level validation rule is a constraint you can define on any business object. It is evaluated whenever the framework attempts to validate...
Read more >
Restrict data input by using validation rules - Microsoft Support
Create a field validation rule​​ On the Fields tab, in the Field Validation group, click Validation, and then click Field Validation Rule. Use...
Read more >
Validation of a list of objects in Spring - Stack Overflow
I found another approach that works. The basic problem is that you want to have a list as your input payload for your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found