I would like Peppercorn or Deform to ignore some HTML fields when
validating a form. I have a Deform widget (here below called
"ExtendedAutocompleteInputWidget") that has two HTML fields: a visible
text input ('person_autocomplete') and an hidden text input ('person'),
the former being for presentation purpose only, the latter holding the
value to be stored), and the following schema:
--- 8< ---
class Person(SequenceSchema):
person = SchemaNode(
String(),
widget=ExtendedAutocompleteInputWidget())
class DemoSchema(Schema):
persons = Person(widget=SequenceWidget(min_len=1))
--- 8< ---
I get the following POSTed items:
('__start__', u'persons:sequence'),
('person', u'1'),
('person_autocomplete', u'John Smith'),
('person', u'2'),
('person_autocomplete', u'Jane Doe'),
('__end__', u'persons:sequence')
Currently, Peppercorn "eats" all HTML fields and transforms this into a
record of 4 items that are all mapped to the 'persons' sequence. I would
like Deform/Peppercorn to only record 2 items (those named 'person' --
like the field). Am I misusing SequenceSchema? (Note that this "bug"
does not appear when the two-HTML-field widget appears in a MappingSchema.)
If I am not misusing anything, how could I work around or fix that?
For now, I call a cleaning method before passing the POSTed data to
'Form.validate' but I would like to move this responsibility to the
widget itself, Deform or Peppercorn.
I suppose that the best option would be to ask each widget if it needs
to remove meaningless HTML fields that it introduced. In
'Field.validate()', before passing the controls to 'peppercorn.parse()',
we could iterate over each widget of the schema and call
'widget.ignore_fields()' if the widget defines such a method. Another
(much easier) option would be to make Peppercorn ignore some fields, for
example those whose name ends with '__no_peppercorn__'. Any idea or
preference?
Cheers,
--
Damien Baty
Have the deserialize() of the widget strip the unnecessary field data
out of the cstruct it returns. See deform's CheckedInputWidget for an
example.
- C
Indeed. I tried to tweak the 'deserialize()' but that did not work at
first. Looking at the template of CheckInputWidget, I saw that it tricks
Peppercorn by enclosing HTML fields in the following Peppercorn marker
fields:
--- 8< ---
<input type="hidden" name="__start__" value="${field.name}:mapping"/>
[...]
<input type="hidden" name="__end__" value="${field.name}:mapping"/>
--- 8< ---
Thanks for the pointer!
--
Damien Baty