Fixed rendering of sequence item "default values" in SequenceWidget #79

Closed
wants to merge 1 commit into
from

3 participants

@aleksandr-rakov

Code example:
class SequenceItem(colander.Schema):
name = colander.SchemaNode(colander.String())
enabled = colander.SchemaNode(colander.Bool(),
default='true',
missing=u'')
class Sequence(colander.SequenceSchema):
items = SequenceItem()
class Schema(colander.Schema):
name = colander.SchemaNode(colander.String())
items = Sequence()

schema = Schema()
form = deform.Form(schema, buttons=('submit',))

When I press "Add Items" in browser "Enabled" checkbox is unchecked.

I fix this problem

@aleksandr-rakov

Another simple code example:

# -*- coding: utf8 -*-
import colander
import deform
import urllib

class seq(colander.SequenceSchema):
            field = colander.SchemaNode(colander.Bool(),
                default='true',
                )
class schema(colander.Schema):
            items = seq()

print "Schema test:"
sch=schema()
print sch['items']['field'].serialize(colander.null)
print
print "Widget test:"
form=deform.Form(sch)
print urllib.unquote(form['items'].widget.prototype(form['items']))

Result:

Schema test:
true

Widget test:
<li
    title="">
  <!-- sequence_item -->
  <span class="deformClosebutton"
        id="deformField3-close"
        title="Remove"
        onclick="javascript:deform.removeSequenceItem(this);"></span>

  <input type="checkbox"
       name="field" value="true"
       id="deformField3"/>

  <!-- /sequence_item -->
</li>

colander.serialize() work well
but
widget will render unchecked checkbox

Please pull my commit to fix it

@tonthon

I've got the same problem here.
the fix provided in the commit solved the issue for me.

Here was my test case

@view_config(renderer='templates/form.pt', name='sequence_of_selects_with_default')
@demonstrate('Sequence of sequence Widgets with default')
def sequence_of_selects(self):
    choices = (('habanero', 'Habanero'), ('jalapeno', 'Jalapeno'),
               ('chipotle', 'Chipotle'))
    class Peppers(colander.SequenceSchema):
        pepper = colander.SchemaNode(
            colander.String(),
            default='jalapeno',
            validator=colander.OneOf([x[0] for x in choices]),
            widget=deform.widget.SelectWidget(values=choices),
            title='Pepper Chooser',
            description='Select a Pepper')
    class Schema(colander.Schema):
        peppers = Peppers()
    schema = Schema()
    form = deform.Form(schema, buttons=('submit',))
    return self.render_form(form)
@tonthon

The following also allow to set the default value for the initial items

@@ -1191,7 +1191,7 @@ class SequenceWidget(Widget):
         # automated testing; finding last node)
         item_field = field.children[0].clone()
         proto = field.renderer(self.item_template, field=item_field,
-                               cstruct=null, parent=field)
+             cstruct=item_field.schema.serialize(colander.null), parent=field)
         if isinstance(proto, string_types):
             proto = proto.encode('utf-8')
         proto = url_quote(proto)
@@ -1224,7 +1224,8 @@ class SequenceWidget(Widget):
         else:
             # this serialization is being performed as a result of a
             # first-time rendering
-            subfields = [ (val, item_field.clone()) for val in cstruct ]
+            subfields = [ (item_field.schema.serialize(val),
+                                    item_field.clone()) for val in cstruct ]

         template = readonly and self.readonly_template or self.template
         translate = field.translate
@tonthon tonthon referenced this pull request in Pylons/deformdemo Sep 20, 2012
Closed

Adding test case for deform #79 issue #14

@tonthon tonthon added a commit to tonthon/deform that referenced this pull request Sep 20, 2012
@tonthon tonthon Fix #79 : default in sequence of widgets 0928991
@tonthon tonthon referenced this pull request in Pylons/deformdemo Sep 20, 2012
Closed

Adding test case for deform #79 issue #15

@tonthon tonthon added a commit to tonthon/deform that referenced this pull request Sep 20, 2012
@tonthon tonthon Fix #79 : default value handling in SequenceWidget's subfields 6951d3e
@mcdonc mcdonc added a commit that closed this pull request Sep 25, 2012
@mcdonc mcdonc - Fixed bug wherein items added to a sequence nor the initial items r…
…endered

  in a sequence would not reflect the correct defaults of the item widget.
  See #79

Closes #79.
7efc3d9
@mcdonc mcdonc closed this in 7efc3d9 Sep 25, 2012
@mcdonc
Pylons Project member

I fixed this differently due to changes on the master, but thank you for the analysis, it was spot-on!

@mcdonc mcdonc added a commit that referenced this pull request Sep 25, 2012
@mcdonc mcdonc fix failing tests after fix for #79 7a125f3
@mcdonc mcdonc referenced this pull request Sep 25, 2012
Closed

Issue #79 #121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment