Python beginner. On pyramid 1.5.1. At this point, I'm just trying to get my app to work functionally.
I'm basically hard coding my forms and just trying to use formencode for validation.
I'm just trying to define a schema, instantiate a validator, and validate the submitted form. I've been at this quite a while, and I can't seem to find an example anywhere on the web, including the project website.
Here's what I've got. I'm quite sure that I'm missing something stupid and simple. It appears that there is no validate function, but I can't find anywhere in the documentation what I'm supposed to call to make the validation happen.
Error:
AttributeError: 'SimpleFormValidator' object has no attribute 'validate'
Code:
class RegistrationSchema(formencode.Schema):
allow_extra_fields = True
password = formencode.validators.PlainText(not_empty=True)
email = formencode.validators.Email(resolve_domain=False)
password = formencode.validators.String(not_empty=True)
@view_config(permission='view', route_name='register',
renderer='templates/user_add.pt')
def user_add(request):
pprint (vars(request.POST))
formvalidator = SimpleFormValidator(RegistrationSchema)
if 'form.submitted' in request.POST and formvalidator.validate():
session = DBSession()
email = form.data['email']
user = User(
password=form.data['password'],
email=form.data['email']
)
session.add(user)
headers = remember(request, email)
redirect_url = request.route_url('main')
return HTTPFound(location=redirect_url, headers=headers)
login_form = login_form_view(request)
logged_in = authenticated_userid(request)
return {
'logged_in': logged_in,
'login_form': login_form,
}
POST:
{'reason': 'Not a form request'}
{'_items': [('_csrf', '208f4c5344cf87fbbe2f79afde7d879b4e3ab7f5'),
('email', '[email protected]'),
('password', 'test'),
('submit', 'Log In')]}
Correct syntax: