Forms: custom clean for DateField

2009-03-06 Thread MarcoS

Hi all,
I've some difficulties in a custom clean function in the following
form:

class SelectDateForm(forms.Form):
firstDate = forms.DateField(widget=AdminDateWidget)
lastDate = forms.DateField(widget=AdminDateWidget)

'cause I want to check if firstDate is minor than lastDate (and
obviously the format of input),
I've defined a custom validation like this:

def clean(self):
super(forms.DateField, self).clean()

data =  self.cleaned_data

if (data['firstDate'] >= data['lastDate']):
raise ValidationError("Error")

# else
return data

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Forms: custom clean for DateField

2009-03-06 Thread MarcoS

 Sorry, i've posted without finishing the message :)

So, if I call clean(self), Django give me this exception for the
following line:

super(forms.DateField, self).clean()

Exception Type: TypeError
Exception Value:
super(type, obj): obj must be an instance or subtype of type

To solve it I tried to replace

super(forms.DateField, self).clean()

with:

super(DateField, self.fields['firstDate']).clean(self)

but I think isn't correct and however the super class doesn't raises
any
exception for input that isn't in a date format.

Please, give me any ideas



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Forms: custom clean for DateField

2009-03-18 Thread Wim Feijen

Hi Marco,

In Django 1.0, you don't need to call clean from clean, Django will do
that automatically for you. :)

So what you can do is:

def clean(self):
data =  self.cleaned_data

if (data['firstDate'] >= data['lastDate']):
raise ValidationError("Error")

# else
return data

That should work.

For more info, see: http://docs.djangoproject.com/en/dev/ref/forms/validation/

Wim


On Mar 6, 11:46 am, MarcoS  wrote:
>  Sorry, i've posted without finishing the message :)
>
> So, if I call clean(self), Django give me this exception for the
> following line:
>
>     super(forms.DateField, self).clean()
>
> Exception Type:         TypeError
> Exception Value:
> super(type, obj): obj must be an instance or subtype of type
>
> To solve it I tried to replace
>
>     super(forms.DateField, self).clean()
>
> with:
>
>     super(DateField, self.fields['firstDate']).clean(self)
>
> but I think isn't correct and however the super class doesn't raises
> any
> exception for input that isn't in a date format.
>
> Please, give me any ideas
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Forms: custom clean for DateField

2009-03-19 Thread johan . uhle

> if (data['firstDate'] >= data['lastDate']):
> raise ValidationError("Error")

it should read
raise forms.ValidationError

If you want to have the error not only as a non_field global error,
but actually added to the fields where the error occured, you can do
the following in the clean(self) method:

error_list=self.errors.get("firstDate")
if error_list is None:
error_list=forms.util.ErrorList()
self.errors["firstDate"]=error_list
error_list.append("Error Message because firstDate > lastDate. Please
correct that!")

I dont know if this is the nicest way, but it works :)

On 18 Mrz., 11:57, Wim Feijen  wrote:
> Hi Marco,
>
> In Django 1.0, you don't need to call clean from clean, Django will do
> that automatically for you. :)
>
> So what you can do is:
>
>     def clean(self):
>         data =  self.cleaned_data
>
>         if (data['firstDate'] >= data['lastDate']):
>             raise ValidationError("Error")
>
>         # else
>         return data
>
> That should work.
>
> For more info, see:http://docs.djangoproject.com/en/dev/ref/forms/validation/
>
> Wim
>
> On Mar 6, 11:46 am, MarcoS  wrote:
>
> >  Sorry, i've posted without finishing the message :)
>
> > So, if I call clean(self), Django give me this exception for the
> > following line:
>
> >     super(forms.DateField, self).clean()
>
> > Exception Type:         TypeError
> > Exception Value:
> > super(type, obj): obj must be an instance or subtype of type
>
> > To solve it I tried to replace
>
> >     super(forms.DateField, self).clean()
>
> > with:
>
> >     super(DateField, self.fields['firstDate']).clean(self)
>
> > but I think isn't correct and however the super class doesn't raises
> > any
> > exception for input that isn't in a date format.
>
> > Please, give me any ideas
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---