Hi all,

I have a form in which the allowed values of a particular field are
constrained by the value of another value in the field. I am using
server side validation of the values which is working OK. The problem I
have is that when a validation error occurs the resulting form displays
the error message against all widgets not just the two involved in the
validation. Perhaps some code would explain things better.


== controllers.py ==

import logging

import cherrypy
from cherrypy import HTTPRedirect

import turbogears
from turbogears import controllers, expose, validate, redirect,
error_handler
from turbogears import widgets as w
from turbogears import validators as v

log = logging.getLogger("form.controllers")


class my_validator(v.FormValidator):

    __unpackargs__ = ('*', 'field_names')

    messages = {
        'my_error_message': 'This is my error message.',
    }

    def validate_python(self, value_dict, state):

        # text2 must be twice text1.
        if value_dict["text2"] != value_dict["text1"] * 2:
            raise v.Invalid(self.message("my_error_message", None),
                            value_dict,
                            state)

#    def _to_python(self, value_dict, state):
#        vd = {}
#        for i in self.field_names:
#            vd[i] = value_dict[i]
#        return vd


class my_schema(v.Schema):
    chained_validators = [my_validator("text1", "text2")]


my_form = w.TableForm(
    fields = [w.TextField(name="text1", label="Text 1",
                          validator=v.Int()),
              w.TextField(name="text2", label="Text 2",
                          validator=v.Int()),
              w.TextField(name="text3", label="Text 3",
                          validator=v.Int()),
             ],
    validator = my_schema(),
    action = 'save',
    submit_text = "Submit")


class Root(controllers.RootController):


    @expose(template="form.templates.form")
    def index(self, tg_errors=None):
        try:
            my_errors = '\n'.join([(str(k) + ' ' + str(i)) for k, i in
tg_errors.items()])
        except:
            my_errors = tg_errors
        return dict(form=my_form, my_errors=my_errors)


    @expose()
    @validate(form=my_form)
    @error_handler(index)
    def save(self, text1, text2, text3):
        raise HTTPRedirect(turbogears.url("/"))


== form.kid ==

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";
xmlns:py="http://purl.org/kid/ns#";
    py:extends="'master.kid'">

<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"
py:replace="''"/>
</head>

<body>

    ${my_errors}
    ${form.display()}

</body>
</html>


Now the problem arises when I for instance enter 1, 4 and 3. The
resultant form is displayed with the string "This is my error message."
next to text1, text2 and text3. How do I control this to have the error
message next to just to text1 and text2?

Thanks in advanced.
Ben.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to