Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

aleksandr-rakov
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Contributor

tonthon commented Sep 18, 2012

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
Copy link
Contributor

tonthon commented Sep 20, 2012

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

@mcdonc
Copy link
Member

mcdonc commented Sep 25, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants