Skip to content

using safestr in web.form.Dropdown._render_option #279

Merged
merged 3 commits into from Feb 22, 2014

2 participants

@nopri
nopri commented Feb 17, 2014

Convert to str (using safestr) before value comparison.

Old behavior (notice the second example):

d = Dropdown(name='a', args=[1,2,3], value=1)
>>> d.render()
u'<select id="a" name="a">\n  <option selected="selected" value="1">1</option>\n  <option value="2">2</option>\n  <option value="3">3</option>\n</select>\n'
>>> 
>>> d = Dropdown(name='a', args=[1,2,3], value='1')
>>> d.render()
u'<select id="a" name="a">\n  <option value="1">1</option>\n  <option value="2">2</option>\n  <option value="3">3</option>\n</select>\n'
>>> 

New behavior (also notice the second example):

>>> d = Dropdown(name='a', args=[1,2,3], value=1)
>>> d.render()
u'<select id="a" name="a">\n  <option selected="selected" value="1">1</option>\n  <option value="2">2</option>\n  <option value="3">3</option>\n</select>\n'
>>> 
>>> d = Dropdown(name='a', args=[1,2,3], value='1')
>>> d.render()
u'<select id="a" name="a">\n  <option selected="selected" value="1">1</option>\n  <option value="2">2</option>\n  <option value="3">3</option>\n</select>\n'
>>> 
@anandology

Look like your patch doesn't work for the multiple select case. could you please take care of that as well?

@nopri
nopri commented Feb 18, 2014

Dear anandology,

Thank you very much for your comment and correction :)

I have updated the code.

Single selection, same type:

>>> print Dropdown(name='a', args=[1,2,3], value=1).render()
<select id="a" name="a">
  <option selected="selected" value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Single selection, different type (notice: args):

>>> print Dropdown(name='a', args=['1',2,3], value=1).render()
<select id="a" name="a">
  <option selected="selected" value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Single selection, different type (notice: value):

>>> print Dropdown(name='a', args=[1,2,3], value='1').render()
<select id="a" name="a">
  <option selected="selected" value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Multiple selection, same type:

>>> print Dropdown(name='a', args=[1,2,3], value=[1,2]).render()
<select id="a" name="a">
  <option selected="selected" value="1">1</option>
  <option selected="selected" value="2">2</option>
  <option value="3">3</option>
</select>

Multiple selection, different type (also see below):

>>> print Dropdown(name='a', args=[1,2,3], value=['1','2']).render()
<select id="a" name="a">
  <option selected="selected" value="1">1</option>
  <option selected="selected" value="2">2</option>
  <option value="3">3</option>
</select>

Multiple selection, different type (mixed value):

>>> print Dropdown(name='a', args=[1,2,3], value=['1',2]).render()
<select id="a" name="a">
  <option selected="selected" value="1">1</option>
  <option selected="selected" value="2">2</option>
  <option value="3">3</option>
</select>

Thank you very much.

@anandology

I don't think it is a good idea to updating self.value when rending an option. May be you should assign it to a new variable instead of updating self.value.

@nopri
nopri commented Feb 18, 2014

Dear anandology,

Again, thank you very much for your correction :)

I have updated the code.

@anandology anandology merged commit d666d65 into webpy:master Feb 22, 2014
@anandology

Thanks @nopri.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.