When all required fields are filled such that they pass validation,
the form can be submitted — and the label of the submit button
changes from Validate to Register.
When submitted, the fields are guaranteed
to be re-validated on the server using exactly the same checks
(actually using the python version of the field checks, from which
the client side validation JavaScript code has been automatically generated).
Note that submission of this demo registration form only
redisplays it with the submitted values (no new user is created).
More explantions at Gizmo Form
(for login details see demo home page).
The definition in python for this registration form is included below,
as part of the source for the /register/ directory — note the
inheritance from gz.fill.register_dir.RegisterDir, that takes
care of tasks such as the handling of the JSON validation
callback defined on the userid field.
Python source for the /register/ directory | register.py#code |
from gz.pub.common import get_publisher, get_request, get_dhi, page
from gz.gizmo.gform import GForm
from gz.gizmo.gspec import Spec
from gz.gizmo.gspec import pattern, equalwidget, shadowwidget, both, length
import gz.fill.register_dir
class RegisterDir(gz.fill.register_dir.RegisterDir):
def define_form(self, gizmoid, dhi):
form = GForm(gizmoid, dhi=dhi, use_tokens=True, check_evt=’onchange’)
form.add_string(‘first’, title=”First Name”)
form.add_string(‘last’, title=”Last Name”)
form.add_string(‘userid’, title=”Username”,
required=True,
hint=(‘Only alphanumeric _- and cannot start with a number. ‘
‘Lowercase (case-insensitive).’),
spec=Spec(pattern(‘^[a-zA-Z_][a-zA-Z0-9_]*$’),
‘Userid is malformed.’, ‘Userid is correctly formed.’),
callback=’json’,
onblur=’javascript:this.value=this.value.toLowerCase();’)
form.add_string(’email’, title=”Email”,
required=True,
hint=’Valid email address, and not used by another user.’,
spec=Spec(pattern(‘^.+@.+\..{2,4}$’),
‘Email is malformed.’, ‘Email is correctly formed.’),
onblur=’javascript:this.value=this.value.toLowerCase();’)
form.add_password(‘passwd1’, title=”Password”,
required=True,
hint=”Case sensitive. At least 4 chars long. At least 1 digit.”,
spec=Spec(shadowwidget(form, ‘passwd2’)))
form.add_password(‘passwd2’, title=”Password Confirm”,
required=True,
hint=”Must be same as first password.”,
spec=Spec(
both(
Spec(equalwidget(form, ‘passwd1’), ‘not equal.’),
Spec(pattern(‘^\d|^\D+\d’), ‘at least 1 digit.’),
Spec(length(4, None), ‘too short.’)),
‘Password is not OK… ‘, ‘Password is OK.’))
form.add_submit(‘register’,
value=’Register’,
value_disabled=”Validate”,
check_evt=False)
return form
def register(self):
dhi = get_dhi()
form = self.define_form(‘reg’, dhi)
if form.is_submitted() and not form.has_errors():
pass # ok, proceed… here, do nothing, just redisplay.
return self.register_page(dhi.x.title, form)
# RegisterDir offers a register_page() method, for easier overriding.
def register_page(self, title, form, **kw):
return page(title, form.render(), template=”register.html”, **kw)