Question

I wrote a custom ErrorList class:

class jQueryUiErrors(ErrorList):
def __unicode__(self):
    return self.as_divs()
def as_divs(self):
    if not self:
        return ''
    return '<div class="ui-state-error ui-corner-all">%s</div>' % ''.join(['<div class="error"><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-alert"></span>%s</div>' % e for e in self])

But can't get it to work. I tried:

class ClientForm(ModelForm):
def __init__(self, *args, **kwargs):
    kwargs_new = {'error_class': jQueryUiErrors}
    kwargs_new.update(kwargs)
    super(ClientForm, self).__init__(*args, **kwargs_new)
class Meta:
    model = Client
    fields = ['first_name', 'last_name', 'owner', 'source', 'contact_status', 'next_contact']

and:

class ClientForm(ModelForm):
def __init__(self, *args, **kwargs):
    self.error_class = jQueryUiErrors
    super(ClientForm, self).__init__(*args, **kwargs)

But without any effect - the errors stay as there where , rendered as list

Im using it in CreateWithInlinesView which is based on CreateView if it has anything to do with anything.

PS Second question: can I change default error_class project wide to format all errors using my class?

Était-ce utile?

La solution

You just have to do some rearangements:

class ClientForm(ModelForm):
    def __init__(self, *args, **kwargs):
        kwargs.update({'error_class': jQueryUiErrors})
        super(ClientForm, self).__init__(*args, **kwargs)
    class Meta:
        model = Client
        fields = ['first_name', 'last_name', 'owner', 'source', 'contact_status', 'next_contact']

Or:

class ClientForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ClientForm, self).__init__(*args, **kwargs)
        self.error_class = jQueryUiErrors
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top