Well now this is working just fine:
class AccountForm(forms.Form):
username = forms.CharField(max_length=100, help_text="Enter Username
of your account", label="Your username")
email = forms.EmailField(max_length=100, help_text="Enter your e-mail
address", label="Your e-mail address")
first_name = forms.CharField(max_length=50, help_text="Enter your
first name(s) and/or initials", label="First name")
last_name = forms.CharField(max_length=50, help_text="Enter your last
name", label="Last name")
pwd = forms.CharField(max_length=100, help_text="Enter your
password", label="Your password", widget=forms.PasswordInput
(render_value=False), required=False)
pwdc = forms.CharField(max_length=100, help_text="Enter your password
again", label="Your password (again)", widget=forms.PasswordInput
(render_value=False), required=False)
def clean(self):
data = self.cleaned_data
if len(data.get("username")) < 4:
raise forms.ValidationError("Username is too short")
if "pwd" in data:
if len(data.get("pwd")) < 7 or len(data.get("pwdc")) <
7:
raise forms.ValidationError("Password has to be
longer than 6
characters")
if data.get("pwd") != data.get("pwdc"):
raise forms.ValidationError("Passwords do not match")
return data
But this (code below) still does not return me any error messages from
form validation.
form = AccountForm(request.POST)
user = request.user
if form.is_valid():
do stuff and save user.
else:
initial_dict = {
'username': user.username,
'email': user.email,
'first_name':user.first_name,
'last_name':user.last_name,
}
message = "was not valid"
accform = AccountForm(initial=initial_dict)
proform = ProfileForm()
context = { 'message':message, 'aform':accform,
'pform':proform, }
return
render_to_response('profile/profile_detail.html', context,
context_instance=RequestContext(request))
Any idea why?
On Jun 8, 7:53 pm, zayatzz <[email protected]> wrote:
> I think i already tried that, Michael, and i got some error that had
> something to do with nonetype... since there is no pwd in data, you
> cant compare if its equal or not to ''
>
> Alan.
>
> On 8 juuni, 17:24, Michael <[email protected]> wrote:
>
> > On Mon, Jun 8, 2009 at 10:09 AM, zayatzz <[email protected]> wrote:
>
> > > So i change pwd and pwdc to required=False and in clean i do
> > > if pwd in data (or if ["pwd"] not in data):
> > > if data.get("pwd") != data.get("pwdc"):
> > > raise forms.ValidationError("Passwords do not match")
> > > return data
>
> > > and i get errorfree clean function?
>
> > > Alan
>
> > No think more simple than that really:
>
> > if cleaned_data['pwd'] != '':
> > if cleaned_data['pwd'] != cleaned_data['pwdc']:
> > raise ...
> > return ...
>
> > There will always be pwd in cleaned_data, if it is empty, it will be an
> > empty string.
>
> > Hope that works for you,
>
> > Michael
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---