pombreda / formalchemy

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

FA failing to restore listbox selection after rendering a form with validation errors #74

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a form a model with n-to-m relationship
2. Render the form, select several records in the relationship multi-select
listbox or array of checkboxes
3. Validate the form, but introducing errors to redisplay form with errors

What is the expected output?
The form should be generated with all previous data populated and
selections saved for reissuing with minimal effort.

What do you see instead?
Generated form is the same, but selections are lost in the relationship field. 

What version of the product are you using?
I'm using formsalchemy version 1.2-py2.5
On what operating system?
GNU/Linux (Fedora 10)

Please provide any additional information below.
It seems that the relationship selection param is saved as an utf-8 codded
list of strings instead of a integers list. It's verified harcoding an
integers list that the selection is correctly repopulated without loss of data.

Original issue reported on code.google.com by robarago@gmail.com on 2 Mar 2009 at 9:27

GoogleCodeExporter commented 9 years ago
I've been using a patch to get around that too, the problem seems to be that
self._value is a string or list of strings after a validation failure, even if 
it
would be another type in a "fresh" fieldset.  I'm not convinced I have a proper 
or
complete fix, but my patch is below:

Index: fields.py
===================================================================
--- fields.py   (revision 714)
+++ fields.py   (working copy)
@@ -457,7 +457,7 @@
         return FieldRenderer._serialized_value(self)

     def _is_checked(self, choice_value):
-        return self._value == choice_value
+        return self._value == choice_value or self._value == str(choice_value) 

     def render(self, options, **kwargs):
         self.radios = []
@@ -480,7 +480,7 @@
         return FieldRenderer._serialized_value(self)

     def _is_checked(self, choice_value):
-        return choice_value in self._value
+        return str(choice_value) in self._value or choice_value in self._value

 class SelectFieldRenderer(FieldRenderer):

Original comment by andr...@gmail.com on 3 Mar 2009 at 10:15

GoogleCodeExporter commented 9 years ago

Original comment by jbel...@gmail.com on 3 Mar 2009 at 10:22

GoogleCodeExporter commented 9 years ago
It seems to work well for CheckBoxSet, but I can't find a similar approach to 
get
SelectFieldRenderer working too.

I'm new at python, so help would be very appreciated... ;-)

Original comment by robarago@gmail.com on 4 Mar 2009 at 2:25

GoogleCodeExporter commented 9 years ago
Although it seems a bit sloppy, this should work:

--- fields.py.orig      2009-03-04 02:26:30.000000000 +0100
+++ fields.py   2009-03-04 03:37:33.000000000 +0100
@@ -457,7 +457,7 @@
         return FieldRenderer._serialized_value(self)

     def _is_checked(self, choice_value):
-        return self._value == choice_value
+        return self._value == choice_value or self._value == str(choice_value) 

     def render(self, options, **kwargs):
         self.radios = []
@@ -480,7 +480,7 @@
         return FieldRenderer._serialized_value(self)

     def _is_checked(self, choice_value):
-        return choice_value in self._value
+        return str(choice_value) in self._value or choice_value in self._value

 class SelectFieldRenderer(FieldRenderer):
@@ -493,7 +493,10 @@
         return FieldRenderer._serialized_value(self)

     def render(self, options, **kwargs):
-        selected = kwargs.get('selected', None) or self._value
+       selected = []
+       if options:
+               t = type(options[0][1])
+               selected = kwargs.get('selected', None) or [t(i) for i in 
self._value]
         return h.select(self.name, h.options_for_select(options, selected=selected),
**kwargs)

Any better idea?

Original comment by robarago@gmail.com on 4 Mar 2009 at 2:44

GoogleCodeExporter commented 9 years ago
fixed in r716.  Thanks for the help tracking this down!

Original comment by jbel...@gmail.com on 4 Mar 2009 at 2:12

GoogleCodeExporter commented 9 years ago

Original comment by jbel...@gmail.com on 4 Mar 2009 at 2:13