You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

43 lines
1.9 KiB

# -*- coding: utf-8 -*-
from django import forms
from captcha.fields import CaptchaField
from . import models
class ReqAvailForm(forms.ModelForm):
"""Форма заказа товара."""
captcha = CaptchaField(label=u'Введите код с картинки')
class Meta:
model = models.ReqAvail
fields = ('name', 'phone', 'message')
def __init__(self, *args, **kwargs):
super(ReqAvailForm, self).__init__(*args, **kwargs)
self.fields['name'].label = u'Ваше имя'
self.fields['name'].widget.attrs['placeholder'] = u'Ваше имя, отчество'
self.fields['phone'].widget.attrs['placeholder'] = u'Ваш номер телефона или e-mail'
self.fields['name'].widget.attrs['width'] = u'Ваше имя, отчество'
def clean(self):
cleaned_data = super(ReqAvailForm, self).clean()
if (cleaned_data.get('name') and cleaned_data.get('message') and cleaned_data.get('phone')) and not(cleaned_data.get('captcha')):
raise forms.ValidationError(u"Проверьте правильность кода проверки")
elif not (cleaned_data.get('name') and cleaned_data.get('message') and cleaned_data.get('phone') and cleaned_data.get('captcha')):
if self.errors.get('phone') and self.data.get('phone'):
raise forms.ValidationError(u"Проверьте правильность ввода контактов")
else:
raise forms.ValidationError(u"Заполните все поля")
return cleaned_data
#name = self.cleaned_data.get('name', '').strip()
#phone = self.cleaned_data.get('phone', '').strip()
#if not name:
# raise forms.ValidationError(u'Укажите Ваше имя.')
#if not phone:
# raise forms.ValidationError(u'Укажите Ваш телефон.')
return self.cleaned_data