How to show a form error only near the respective field

0 views
Skip to first unread message

Sanjay

unread,
Dec 9, 2006, 7:01:18 AM12/9/06
to TurboGears
Hi All,

I am using a form widget, and have defined a form level custom
validator to verify two fields combined. In case of error, I want to
show error message only beside one of these two fields. But the error
message comes beside every field in the form.

Needing suggestion. The problem seems similar to the one discussed at

http://groups.google.co.in/group/turbogears/browse_thread/thread/d238edd1dc04b774/ef970f4fa395516c

thanks
sanjay

Alberto Valverde

unread,
Dec 9, 2006, 11:28:04 AM12/9/06
to turbo...@googlegroups.com

On Dec 9, 2006, at 1:01 PM, Sanjay wrote:

>
> Hi All,
>
> I am using a form widget, and have defined a form level custom
> validator to verify two fields combined. In case of error, I want to
> show error message only beside one of these two fields. But the error
> message comes beside every field in the form.

Take a look at the code of FieldsMatch from formencode (for example).
As the validator works on the whole form you need to build an error
dict and place the error in the field's key you want the error to
appear at.

something like:

def validate_python(self, field_dict, state=None):
errors = {}
if there_is_an_error:
errors['foo'] = "Invalid input at foo"
raise Invalid("The form contains an error", field_dict,
state, error_dict=errors)

Alberto

Sanjay

unread,
Dec 11, 2006, 1:45:04 AM12/11/06
to TurboGears
Hi Alberto, thanks a lot! Works perfectly.

sanjay

Reply all
Reply to author
Forward
0 new messages