Tuesday, September 12, 2017

Re: Email validation in form

"asdf@asdf" is not a valid email so form.is_valid returns False and form.save is never called



On Tue, Sep 12, 2017 at 4:52 AM, Moreplavec <stanislav.vasko@gmail.com> wrote:
Greetings,

i have strange problem with my model form. I have simple form for taking orders, model looks like:

class Order(models.Model):
    name = models.CharField(max_length=50, blank=False, verbose_name='Jméno')
    surname = models.CharField(max_length=50, blank=False, verbose_name='Příjmení')
    email = models.EmailField(max_length=254, blank=True)
    phone = models.CharField(max_length=50, verbose_name='Telefon', blank=True)
    coursedate = models.ForeignKey(CourseDate, verbose_name='Termín')
    note = models.CharField(max_length=500, blank=True, verbose_name='Poznámka')
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    def __str__(self):
        return '%s, %s' % (self.surname, self.coursedate.course.name)

and template (bootstrap, modal):

 
{{ form.non_field_errors }}
<form action="." method="POST">{% csrf_token %}
                        {{ form.non_field_errors }}
                        {% for hidden in form.hidden_fields %}
                            {{ hidden }}
                        {% endfor %}
                        {% for field in form.visible_fields %}
                        <div class="form-group">
                            {% if field.field.required %}
                            {{ field.errors }}
                                {{ field.label }}<span class="special_class">*</span>{{ field }}
                            {% else  %}
                                {{ field.label }} {{ field }}
                            {% endif %}
                        </div>
                        {% endfor %}
                    
                    </div>
                    <div class="modal-footer">
                        <a href="#" data-dismiss="modal">Close</a>
                        <button type="submit" class="btn btn-primary">Send</button>
                    </div>
                    </form> 

And in model:

def serve(self, request):
        from crm.forms import OrderFormNew
        if request.method == 'POST':
            form = OrderFormNew(request.POST)
            if form.is_valid():
                form.date_changed = datetime.date.today()
                form.save()
                return render(request, 'courses/course_web.html', {
                    'page': self,
                    'form': form,
                })
        else:
            form = OrderFormNew()
            form.fields["coursedate"].queryset = CourseDate.objects.filter(course=self.course).order_by('date_start')
        return render(request, 'courses/course_web.html', {
            'page': self,
            'form': form,
        })

 
All fields work fine, but when i insert email, for example "asdf@asdf" form is taken away but not saved to model. So Django wont validate, but form takes it. How to make form to use same rules as Django? When i use email "test@test.com" all works fine and data are in model saved.

Thanks for help, maybe it's to much things together while using pop-up + validation and i'm mixing things together and error is shown somewhere, but lost in code :)

Moreplavec

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fabad5ea-8535-483c-895f-ebb0d90e5c1a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei2qaVXFvZsyJtasn0Vspt%2B9g%3DQOwAO9A%2Bk2vNthQZ%3DDUA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment