add form validation

validate input in ddportfolio controller
input validation schema in model/form.py
error message display and style added
This commit is contained in:
Jan Dittberner 2009-01-21 00:11:19 +01:00
parent a6d4a99050
commit d86965304f
4 changed files with 38 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import logging import logging
from ddportfolioservice.lib.base import * from ddportfolioservice.lib.base import *
from ddportfolioservice.model.form import *
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -11,3 +12,12 @@ class DdportfolioController(BaseController):
# return render('/some/template.mako') # return render('/some/template.mako')
# or, Return a response # or, Return a response
return render('/showform.mako') return render('/showform.mako')
def handle_post(self):
schema = DeveloperData()
try:
form_result = schema.to_python(request.params)
except formencode.validators.Invalid, error:
c.messages = { 'errors' : error.unpack_errors() }
return render('/showform.mako')
return ["done"]

View file

@ -0,0 +1,8 @@
import formencode
class DeveloperData(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
name = formencode.validators.String(not_empty=True)
email = formencode.validators.Email(not_empty=True)
username = formencode.validators.PlainText(not_empty=True)

View file

@ -0,0 +1,4 @@
.errormsg {
font-size: 80%;
color: red;
}

View file

@ -3,16 +3,29 @@
<html> <html>
<head> <head>
<title>${_('Debian Developer Portfolio')}</title> <title>${_('Debian Developer Portfolio')}</title>
${h.stylesheet_link_tag('style')}
</head> </head>
<body> <body>
${h.start_form(h.url_for(action='handle_post'), method='post')} ${h.start_form(h.url_for(action='handle_post'), method='post')}
<fieldset id="ddportfolio"> <fieldset id="ddportfolio">
<legend>${_('Debian Developer Portfolio')}</legend> <legend>${_('Debian Developer Portfolio')}</legend>
<label for="name">${_('Name:')}</label><br /> <label for="name">${_('Name:')}
% if 'name' in c.messages['errors']:
<br /><span class="errormsg">${c.messages['errors']['name']}</span>
% endif
</label><br />
${h.text_field('name', value=request.params.get('name', None))}<br /> ${h.text_field('name', value=request.params.get('name', None))}<br />
<label for="email">${_('E-Mail address:')}</label><br /> <label for="email">${_('E-Mail address:')}
% if 'email' in c.messages['errors']:
<br /><span class="errormsg">${c.messages['errors']['email']}</span>
% endif
</label><br />
${h.text_field('email', value=request.params.get('email', None))}<br /> ${h.text_field('email', value=request.params.get('email', None))}<br />
<label for="username">${_('User name:')}</label><br /> <label for="username">${_('User name:')}
% if 'username' in c.messages['errors']:
<br /><span class="errormsg">${c.messages['errors']['username']}</span>
% endif
</label><br />
${h.text_field('username', value=request.params.get('username', None))}<br /> ${h.text_field('username', value=request.params.get('username', None))}<br />
${h.submit(value=_('Build DD Portfolio URLs'))} ${h.submit(value=_('Build DD Portfolio URLs'))}
</fieldset> </fieldset>