def form_view(context, request): myform = form.Form(context, request) # define fields for form myform.fields = form.Fieldset( form.TextField('title', title=u'Title'), # field title form.TextAreaField( 'description', title=u'Description', missing=u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified form.TextField( 'email', title=u'E-Mail', description=u'Please provide email address.', validator=form.Email(), # email validator ), ) # form actions def cancelAction(form): raise HTTPFound(location='.') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return pprint(data) form.context.title = data['title'] form.context.description = data['description'] form.message('Content has been updated.', 'info') raise HTTPFound(location='.') myform.buttons.add_action('Update', action=updateAction) myform.buttons.add_action('Cancel', action=cancelAction) # form default values myform.content = { 'title': context.title, 'description': context.description } # prepare form myform.update() # render form res = myform.render() # optional, render form in layout layout = view.LayoutRenderer('') return layout(context, request, res)
def contact_us(context, request): contactform = form.Form(context, request) contactform.label = 'Contact us' contactform.fields = form.Fieldset( form.TextField('fullname', title=u'First & Last Name'), form.TextField('phone', title=u'Telephone number', description=u'Please provide telephone number', validator=Telephone()), form.TextField('email', title=u'Your email', description=u'Please provide email address.', validator=form.Email()), form.TextAreaField( 'subject', title=u'How can we help?', missing=u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified ) # form actions def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return # form.context is ... form.context.fullname = data['fullname'] form.context.phone = data['phone'] form.context.email = data['email'] form.context.subject = data['subject'] # You would add any logic/database updates/insert here. # You would probably also redirect. log.info('The form was updated successfully') form.message('The form was updated successfully') contactform.buttons.add_action('Contact us', action=updateAction, actype=form.AC_PRIMARY) contactform.buttons.add_action('Cancel', action=cancelAction) # form default values contactform.content = {} return contactform()
def form_view(context, request): myform = form.Form(context, request) # define fields for form myform.fields = form.Fieldset( form.TextField( 'title', title = u'Title'), # field title form.TextAreaField( 'description', title = u'Description', missing = u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified form.TextField( 'email', title = u'E-Mail', description = u'Please provide email address.', validator = form.Email(), # email validator ), ) # form actions def cancel_action(form): form.message('Cancel button', 'info') def update_action(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return pprint(data) form.message('Content has been updated.', 'info') return HTTPFound(location='.') myform.buttons.add_action('Update', action=update_action, actype=ptah.form.AC_PRIMARY) myform.buttons.add_action('Cancel', action=cancel_action) # form default values myform.content = {'title': 'Test title', 'description': 'Context description'} # render form myform.update() return {'view': myform}
class MyForm(form.Form): view.pview('test-form.html', context=cms.Content) # define fields for form fields = form.Fieldset( form.TextField('title', title=u'Title'), # field title form.TextAreaField( 'description', title=u'Description', missing=u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified form.TextField( 'email', title=u'E-Mail', description=u'Please provide email address.', validator=form.Email(), # email validator ), ) # form default values def form_content(self): return { 'title': self.context.title, 'description': self.context.description } @form.button('Update') def update_handler(self): data, errors = self.extract() if errors: self.message(errors, 'form-error') return pprint(data) self.context.title = data['title'] self.context.description = data['description'] self.message('Content has been updated.', 'info') raise HTTPFound(location='.') @form.button('Cancel') def cancel_handler(self): raise HTTPFound(location='.')
def contact_us(context, request): contactform = form.Form(context, request) contactform.fields = form.Fieldset( form.TextField( 'fullname', title = u'First & Last Name'), form.TextField( 'phone', title = u'Telephone number', description=u'Please provide telephone number', validator = Telephone()), form.TextField( 'email', title = u'Your email', description = u'Please provide email address.', validator = form.Email()), form.TextAreaField( 'subject', title = u'How can we help?', missing = u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified ) # form actions def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return # form.context is ... form.context.fullname = data['fullname'] form.context.phone = data['phone'] form.context.email = data['email'] form.context.subject = data['subject'] # You would add any logic/database updates/insert here. # You would probably also redirect. log.info('The form was updated successfully') form.message('The form was updated successfully') contactform.label = u'Contact us' contactform.buttons.add_action('Update', action=updateAction) contactform.buttons.add_action('Cancel', action=cancelAction) # form default values contactform.content = {} # prepare form result = contactform.update() if isinstance(result, HTTPFound): return result # render form into HTML rendered_form = contactform.render() # query for links to populate links box links = ptah.get_session().query(models.Link) #include library dependencies ptah.include(request, 'bootstrap') # render all the included libraries into html rendered_includes = ptah.render_includes(request) # render messages rendered_messages = ptah.render_messages(request) return {'links': links, 'rendered_form': rendered_form, 'rendered_messages': rendered_messages, 'rendered_includes': rendered_includes}