jrief / django-angular

Let AngularJS play well with Django
http://django-angular.awesto.com/
MIT License
1.23k stars 293 forks source link

Form erros rendered as ul and input values doesn't affect validation #287

Closed bene25 closed 7 years ago

bene25 commented 7 years ago

Hi there Django 1.10.5, python 3.5

I have form:

class LightboxOrderFormTest(NgFormValidationMixin, NgModelForm):
    class Meta:
        model = LightboxOrder
        fields = ['lightbox_client_email']

        labels = {
            'lightbox_client_email': _('email'),
        }

        error_messages = {
            'lightbox_client_email': {'required': "You can't have an empty dasdasdadlist item"}
        }

view:

def test_validation(request):
    if request.method == 'GET':
        form = LightboxOrderFormTest(auto_id=True)
        return render(request, 'test_validation.html', {'form': form})

url:

url(r'^test_validation', views.test_validation, name='test_validation'),

and hml template test_validation.html:

{% block page_content %}
    <div class="container">
        <form name="{{ form.form_name }}" novalidate>
            {{ form }}
            <input type="submit" value="Submit"/>
        </form>

    </div>
    {% autoescape off %}{% endautoescape %}

{% endblock %}

But form rendered with errors displayed as ul and invalid data input doesn't take affect on this messages. Any help. Thanks.

jrief commented 7 years ago

I don't understand your issue.

adrienbrunet commented 7 years ago

You should use {{ form.as_div }} to have the expected behavior. Is that what you want?

bene25 commented 7 years ago

Thanks for answer)) {{ form.as_div }} hides input field (EmailField) and there are only submit button left on page without errors and any other information.

Ok, let me describe my situation again:) I followed this doc http://django-angular.readthedocs.io/en/latest/angular-form-validation.html

And now I have form

class TestForm(NgFormValidationMixin, NgModelForm):
    class Meta:
        model = LightboxOrder
        fields = ['lightbox_client_email']

        labels = {
            'lightbox_client_email': _('testemail'),
        }

        error_messages = {
            'lightbox_client_email': {
                'required': _("This writer's name is too long test."),
            },
        }

View:

class ContactFormView(TemplateView):
    template_name = 'viveska/remont_reklamnoy_produkcii.html'

    def get_context_data(self, **kwargs):
        context = super(ContactFormView, self).get_context_data(**kwargs)
        context.update(form=TestForm(auto_id=True))
        return context

url:

url(r'^remont_reklamnoy_produkcii', ContactFormView.as_view(), name='remont_reklamnoy_produkcii'),

and remont_reklamnoy_produkcii.html:

{% extends 'viveska/base.html' %}

{% block page_content %}
    <div class="container">
        <form name="{{ form.form_name }}" novalidate>
            {{ form.as_div }}
            <input type="submit" value="Submit"/>
        </form>
    </div>
    {% autoescape off %}{% endautoescape %}
{% endblock %}

firstly html was

<form name="{{ form.form_name }}" novalidate>
            {{ form }}
            <input type="submit" value="Submit"/>
        </form>

and it looks like this. Submit button does nothing. shot1

after I have changed to

<form name="{{ form.form_name }}" novalidate>
            {{ form.as_div }}
            <input type="submit" value="Submit"/>
        </form>

it become looks like this. Submit button also does nothing . shot2

adrienbrunet commented 7 years ago

Submit button does nothing because you have a novalidate tag in your form. It's because you're expected to handle the post call with angular itself, not django. try to add an ng-click tag with a function to handle your POST call. That's the whole point.

The as_div is not what you were looking for. I did not read correctly your issue the first time. It's only used with the bootstrap styling (and mixin).

Cheers

reopen this ticket if you found a bug but I guess it was only a misunderstanding.