Re: htmlfill, errors and examples

michelts <[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Hi Ian,

Sorry for the late response, I done some work over my idea and I want to
share it now:

1) I change the schema.Schema class, I add the examples classmethod, this
method seek on each field by the attribute "example" and returns a
dictionary with these examples, if there isn't, returns None for the field:

...
    def examples(cls):
        return dict([(x, getattr(y, 'example', None)) for x, y in
cls.fields.items()])
    examples = classmethod(examples)
...

2) I change the htmlfill.FillingParser class, I add the keyword "examples"
at the constructor, and I changed the "handle_error" method as above:

...
        error = self.errors.get(name, '')
        if error:
            error = self.error_formatters[formatter](error)
            self.write_text(error)
        else:
            example = self.examples.get(name, '')
            if example:
                self.write_text(default_example_formatter(example))
...

3) The function default_example_formatter is defined in the htmlfill scope:

...
def default_example_formatter(example):
    """
    Formatter that escapes the example message, wraps it in a span with
    class ``example-message``, with parentesis and adds a ``<br>``
    """
    return '<span class="example-message">(Ex.: %s)</span><br/>\n' % (
        html_quote(example))
...
4) When I use the FillingParser, I must pass the keyword "examples" to the
constructor, the examples can be catch like above:

...
class MySchema(schema.Schema):
     name = validators.String(not_empty=True)
     birth = validators.SomeDateValidator(example='1984-02-26')

defaults = ...
errors = ...
examples = MySchema.examples() # will returns {'name':'None,
birth:'1984-02-26'}
htmlfill.FillingParser(defaults, errors=errors, examples=examples)

5) Suppose I feed the filling parser with the html code above:

# the raw code
Name: <input type="text" name="name"> <form:error name="name">
Birth: <input type="text" name="birth"> <form:error name="birth">

# the first time the form is loaded
Name: _____________
Birth: _____________ <span class="example-message">(ex: 1984-02-26)</span>

# if there was an error
Name: __________ <span class="error-message">Please, enter a value</span>
Birth: 1984-02-31 <span class="error-message">the data is not valid</span>

It is working now, I only must take a look at the formatters to be conform
the errors formatters. I can send the svndiff but I forgot the svn address
and the formencode.org site is down now.

See that this way you can specify the examples on the schema definition or
on the validator class, if you don't want examples to be shown, you just do
not set the attribute "example" on the validator or you set this attribute
to None.

I have just a little question about classmethod, what do you preffer Ian?
With the @classmethod decorator or with the examples = classmethod(examples)
function?

Tell me if I wasn't clear,
Thanks for the help!

michelts wrote:
> > Hi guys,
> >
> > I use formencode to validate my forms, I do the form presentation by
> > hand (I don't use any form generator), in my page I insert some tags
> > to be replaced by the error message (<form:error name="field">).
> >
> > I had an idea, I want to be able to replace the tag by an example when
> > there is not error (the first time the form is loaded). Look:
> >
> >     # the raw code
> >     Date: <input type="text" name="date"> <form:error name="date">
> >
> >     # the first time the form is loaded
> >     Date: _____________ (ex: 2006/09/20)
> >
> >     # if there was an error
> >     Date: 2006/02/31 the data is not valid



Hmm... right now there is <form:iferror>, but it almost seems like you
> want <form:ifnoterror> or something along those lines.  E.g.:
>
>    Date: <input type="text" name="date"> <form:error name="date">
>    <form:iferror name="not date">ex: 2006/09/20</form:if-error>
>
> I've added the "not field_name" form to the FormEncode trunk.  Will this
> work for you?
>


-- 
Michel Thadeu Sabchuk
Curitiba - Brasil

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.