Hi Jonathan,
You can get a formencode.api.Invalid object with try-catch directive
in controller.
The object contains error messages, so you can use it in templates.
For example, you can pass "error_dict" to templates like this.
----
from formencode.api import Invalid
class SomeController(BaseControllre):
....
try:
converted = schema.PageSchema().to_python(request.params)
except Invalid, e:
c.error_dict = e.error_dict
render('/edit.mako')
If you want to output error messages automatically,
formencode.htmlfill is useful.
----
from formencode.api import Invalid
from formencode import htmlfill
class SomeController(BaseControllre):
....
try:
converted = schema.PageSchema().to_python(request.params)
except Invalid, e:
htmlfill.render(render('/edit.mako'), defaults=request.params,
errors=c.error_dict)
In default, htmlfill puts error messages just after the input tag.
If you want some other options, please check the source of
formencode.htmlfill.render.
----
Junya Hayashi <[EMAIL PROTECTED]>
On 3ζ10ζ₯, εεΎ1:56, Jonathan Vanasco <[EMAIL PROTECTED]> wrote:
> I made a FormValidator subclass called DatefieldsValidator (below) ,
> which validates a day/month/year field
>
> how should I push errors from there into the template ? can helpers
> be used?
>
> ---
> class DatefieldsValidator(formencode.validators.FormValidator):
> """
> Checks if a date is valid
>
> DatefieldsValidator('birth_year','birth_month','birth_day',
> require_years_ago=13)
> """
> # tells UniqueNameValidator where to unpack the args passed to
> __init__
> __unpackargs__ = ('*','field_names')
> field_names = None
>
> require_years_ago= None
>
> messages = {
> 'date_invalid':"The date you submitted (%(date_string)s) is
> not a real date.",
> 'date_too_late':"The date you submitted (%(date_string)s) is
> not before the cutoff date (%(latest_date_string)s).",
> }
>
> def validate_python(self, field_dict, state):
> print self.__dict__
> date_string= "%04d-%02d-%02d" % (
> int(field_dict[self.field_names[0]]),
> int(field_dict[self.field_names[1]]),
> int(field_dict[self.field_names[2]]),
> )
> date= None
> try:
> date= time.strptime(date_string,"%Y-%m-%d")
> except:
> msg = self.message('date_invalid', state, date_string=
> date_string)
> raise formencode.api.Invalid(msg, field_dict, state,
> error_dict={'form':msg})
>
> if self.require_years_ago:
> latest_date_string= datetime.date.today() -
> relativedelta(years= self.require_years_ago)
> latest_date= time.strptime( "%s"%latest_date_string, "%Y-
> %m-%d")
> timedelta= time.mktime(latest_date) - time.mktime(date)
> if timedelta < 0:
> msg = self.message('date_too_late', state,
> date_string= date_string, latest_date_string= latest_date_string)
> raise formencode.api.Invalid(msg, field_dict, state,
> error_dict={'form':msg})
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---