Commit 70e49e75 authored by Romain Courteaud's avatar Romain Courteaud

Implement a working version of FormBox validator.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20041 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 27b08a00
...@@ -38,10 +38,11 @@ from Globals import get_request ...@@ -38,10 +38,11 @@ from Globals import get_request
from Products.PythonScripts.Utility import allow_class from Products.PythonScripts.Utility import allow_class
from Products.PythonScripts.standard import url_quote_plus from Products.PythonScripts.standard import url_quote_plus
from Products.Formulator.Errors import FormValidationError, ValidationError
import string import string
from zLOG import LOG from zLOG import LOG, WARNING, DEBUG, PROBLEM
class FormBoxWidget(Widget.Widget): class FormBoxWidget(Widget.Widget):
""" """
...@@ -65,11 +66,12 @@ class FormBoxWidget(Widget.Widget): ...@@ -65,11 +66,12 @@ class FormBoxWidget(Widget.Widget):
""" """
property_names = Widget.Widget.property_names + [ property_names = Widget.Widget.property_names + [
'form_id', \ 'formbox_target_id', \
] ]
form_id = fields.StringField( # This name was changed to prevent naming collision with ProxyField
'form_id', formbox_target_id = fields.StringField(
'formbox_target_id',
title='Form ID', title='Form ID',
description=( description=(
"ID of the form which must be rendered in this box."), "ID of the form which must be rendered in this box."),
...@@ -84,20 +86,53 @@ class FormBoxWidget(Widget.Widget): ...@@ -84,20 +86,53 @@ class FormBoxWidget(Widget.Widget):
default="", default="",
required=0) required=0)
def render(self, field, key, value, REQUEST): def render(self, field, key, value, REQUEST):
""" """
Render a form in a field Render a form in a field
""" """
here = REQUEST['here'] here = REQUEST['here']
form = getattr(here, field.get_value('form_id')) try:
return form() form = getattr(here, field.get_value('formbox_target_id'))
except AttributeError:
LOG('FormBox', WARNING,
'Could not get a form from formbox %s in %s' % \
(field.id, field.aq_parent.id))
return ''
return form(REQUEST=REQUEST)
class FormBoxEditor:
"""
A class holding all values required to update the object
"""
def __init__(self, field_id, result):
self.field_id = field_id
self.result = result
def view(self):
return self.__dict__
def __call__(self, REQUEST):
pass
def render_view(self, field, value): def edit(self, context):
""" context.edit(**self.result[0])
Display a form in a field for encapsulated_editor in self.result[1]:
encapsulated_editor.edit(context)
def as_dict(self):
"""
This method is used to return parameter dict.
XXX This API is probably not stable and may change, as some editors are used to
edit multiple objects.
""" """
return self.render() result_dict = self.result[0]
for encapsulated_editor in self.result[1]:
if hasattr(encapsulated_editor, 'as_dict'):
result_dict.update(
encapsulated_editor.as_dict())
return result_dict
allow_class(FormBoxEditor)
class FormBoxValidator(Validator.Validator): class FormBoxValidator(Validator.Validator):
""" """
...@@ -105,11 +140,30 @@ class FormBoxValidator(Validator.Validator): ...@@ -105,11 +140,30 @@ class FormBoxValidator(Validator.Validator):
the result as a single variable. the result as a single variable.
""" """
property_names = Validator.Validator.property_names property_names = Validator.Validator.property_names
message_names = Validator.Validator.message_names + \
['form_invalidated',]
form_invalidated = "Form invalidated."
def validate(self, field, key, REQUEST): def validate(self, field, key, REQUEST):
form = field.aq_parent # XXX hardcoded acquisition
form = getattr(form, field.get_value('form_id')) here = field.aq_parent.aq_parent
return form.validate(REQUEST) formbox_target_id = field.get_value('formbox_target_id')
# Get current error fields
current_field_errors = REQUEST.get('field_errors', [])
# XXX Hardcode script name
result, result_type = here.Base_edit(formbox_target_id, silent_mode=1)
if result_type == 'edit':
return FormBoxEditor(field.id, result)
elif result_type == 'form':
formbox_field_errors = REQUEST.get('field_errors', [])
current_field_errors.extend(formbox_field_errors)
REQUEST.set('field_errors', current_field_errors)
getattr(here, formbox_target_id).validate_all_to_request(REQUEST)
else:
raise NotImplementedError, result_type
FormBoxWidgetInstance = FormBoxWidget() FormBoxWidgetInstance = FormBoxWidget()
FormBoxValidatorInstance = FormBoxValidator() FormBoxValidatorInstance = FormBoxValidator()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment