Re: Validating two forms in one

2008-11-21 Thread Malcolm Tredinnick


On Fri, 2008-11-21 at 10:28 +0100, Florencio Cano wrote:
> Hello all,
> I have two models called User and Responsible. When I add a
> Responsible automatically a User is added. I have only one form to add
> a User and a Responsible.
> 
> class NewUserResponsibleForm(forms.Form):
> username = forms.CharField(max_length=30)
> password = forms.CharField(max_length=20, widget=forms.PasswordInput())
> confirm_password = forms.CharField(max_length=20,
> widget=forms.PasswordInput())
> email = forms.EmailField(max_length=30)
> name = forms.CharField(max_length=64)
> responsible_boss = forms.BooleanField(required=False)
> 
> With the username and the password a User will be created. With the
> others fields a Responsible will be created.
> The problem is that I want to check that the username is unique and if
> it is not, show the error in this form. If I check this form with
> is_valid() the username is not checked against User and I can't use
> the attribute unique=True in forms.Form.

All you need to do is write a clean_username() method that does the
validation you need. You can do any sort of custom checking on a form
field that you like by writing such a method. The documentation for this
is at 

http://docs.djangoproject.com/en/dev/ref/forms/validation/

Regards,
Malcolm



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Validating two forms in one

2008-11-21 Thread Florencio Cano

Hello all,
I have two models called User and Responsible. When I add a
Responsible automatically a User is added. I have only one form to add
a User and a Responsible.

class NewUserResponsibleForm(forms.Form):
username = forms.CharField(max_length=30)
password = forms.CharField(max_length=20, widget=forms.PasswordInput())
confirm_password = forms.CharField(max_length=20,
widget=forms.PasswordInput())
email = forms.EmailField(max_length=30)
name = forms.CharField(max_length=64)
responsible_boss = forms.BooleanField(required=False)

With the username and the password a User will be created. With the
others fields a Responsible will be created.
The problem is that I want to check that the username is unique and if
it is not, show the error in this form. If I check this form with
is_valid() the username is not checked against User and I can't use
the attribute unique=True in forms.Form.
I have tried gathering the info with this form and then passing the data to:

class UserForm(forms.ModelForm):
class Meta:
model = User

and the checking this form with is_valid(). But now the error is in
that form and not in NewUserResponsibleForm that is the form I show. I
would like to pass the errors from UserForm to NewUserResponsibleForm
or some way of doing what I'm trying.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---