I actually don't want to show the entered values - I basically want
the original form with values prior to the user submitting the form -
but with the validation errors that prompt them to enter new values in
the proper fields. I have already overridden the particular field's
clean method (clean_guests), but I don't understand how to preserve
the field's original value.
class RegistrationForm(ModelForm):
guests = forms.IntegerField(error_messages={'invalid': 'Please
enter a number greater than zero.'})
# enforce role/price limits
def clean_guests(self):
data = self.cleaned_data['guests']
if self.instance.price.parent.price_limit:
spaces_available = self.instance.price.spaces_available
delta = data - self.instance.guests
if delta > 0:
if spaces_available == 1 and delta > 1:
print self.instance.guests
raise forms.ValidationError("There is only 1 more
space available for this role.")
elif spaces_available > 1 and delta >
spaces_available:
raise forms.ValidationError("There are only %s
more spaces available for this role." % spaces_available)
else:
raise forms.ValidationError("Sorry, there are no
more spaces available for this role.")
return data
class Meta:
model = JosJeventsRegistration
fields = ['id', 'guests']
My view creates the form with the POST data in the standard fashion:
if request.method == 'POST':
fs = RegistrationFormSet(request.POST)
if fs.is_valid():
...
Which passes the form with the same post data and some error messages
back to the user.
On Jun 22, 1:14 am, Kevin Renskers <i...@bolhoed.net> wrote:
> If I understand this correctly, then you want something like this?
>
> 1) Show a form to edit something
> 2) User changes fields, submits the form
> 3) If the form was not valid, not only show the errors and the form with the
> entered values, but also the *old* values, pre-edit.
>
> I think your best bet is to create custom valid_field() validation functions
> in the form class, which access the form instance to show the old values in
> the error message.
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment