pombreda / formalchemy

Automatically exported from code.google.com/p/formalchemy
MIT License
0 stars 0 forks source link

CheckBoxRender doesn't send the good value to h.check_box #89

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create a renderer
2. set some value
3. ???
4. render

What is the expected output? What do you see instead?
True or false depending on whats inside the model or depending on what we
set before the validation

formalchemy.fields line 301 sends a string for the "checked" argument
self._values to checkbox
formalchemy.helpers line 130 receive a boolean for checked...

then if checked: is always true because it is a string...so not None

Original issue reported on code.google.com by lamers...@gmail.com on 31 Mar 2009 at 8:19

GoogleCodeExporter commented 9 years ago
here is a possible fix

Definition: h.check_box(name, value='1', checked=False, **options)
Source:
def check_box(name, value="1", checked=False, **options):
    """
    Creates a check box.
    """
    o = {'type': 'checkbox', 'name_': name, 'id': name, 'value': value}
    o.update(options)
    if checked.upper()[0] == 'T':
        o["checked"] = "checked"
    return tag("input", **o)

Original comment by lamers...@gmail.com on 31 Mar 2009 at 8:21

GoogleCodeExporter commented 9 years ago
or 
Source:
class CheckBoxFieldRenderer(FieldRenderer):
    """render a boolean value as checkbox field"""
    def render(self, **kwargs):
        return h.check_box(self.name, True, checked=self._value.upper()[0]=='T',
**kwargs)
    def _serialized_value(self):
        if self.name not in self._params:
            return None
        return FieldRenderer._serialized_value(self)
    def deserialize(self):
        if self._serialized_value() is None:
            return False
        return FieldRenderer.deserialize(self)

Original comment by lamers...@gmail.com on 31 Mar 2009 at 8:23

GoogleCodeExporter commented 9 years ago
and for a cleaner way, just parse the self._value into a function that return
None
True
or 
False

Original comment by lamers...@gmail.com on 31 Mar 2009 at 8:24

GoogleCodeExporter commented 9 years ago
Fixed by safe-eval-ing the _value.

Original comment by jbel...@gmail.com on 8 Apr 2009 at 7:29

GoogleCodeExporter commented 9 years ago
Thank you :)

Original comment by lamers...@gmail.com on 8 Apr 2009 at 9:56