You only have to reset the values of properties bound to a HTML ‘checkbox’ or
a HTML ‘select’ list. Because the browers only send the selected values for
these controls and so you can’t know which property is desactivated. It would
be easier if they send all the values with for example an associated activated /
desactivated flag but it’s not the case.
In this context, reseting the editor property values doesn’t mean to set them
to their initial values again. It means setting them to their default,
« no selected » state: often ‘False’ for a checkbox and empty list (« [] »)
for a select.
‘self.age’ being bound to an input(‘text’) control its value is always sent
by the browers and so always correctly stored in the property each time the
form is submitted. You don’t have to reset it beforehand.
> And in fact, when I attempted to set a form field to a numerical value
> in an editor property that had the same validations rules as age does
> here, I was told that it had to be a string.
An editor property is the link between the HTML control and your business property:
it receives the value sent by the browser, which is always a string, and can apply
validations and a convertion on it.
As described in
http://www.nagare.org/trac/wiki/CallbacksAndForms#the-editor-property-objects,
an editor property has:
- a ‘input’ attribut, the string sent by the browser
- a ‘value’ attribut, the value after the validation and conversion of the ‘input’
- a ‘error’ attribut, the error msg if the validation failed
When you call the 'commit()’ method, the ‘value’ attribut is wrote back to your
business object.
When you wrote ‘self.age(« 20 »)’ you set the ‘input’ attribut so the value must
be a string. If you want you could do a ‘self.age.value = 20'
> Next, it seems that the reset values for editor properties should be
> obtained from some initialization code in the target object. Something
> like:
>
> self.big(self.target.initializer_for['big']())
>
> I know that is verbose, but it seems to force initialization to happen
> in one place not two.
If you really want to memorize the initial value, you can also do:
def __init__(self, target):
super(FormEditor, self).__init__(target, self.fields)
self.big_initial_value = self.big.value
But, as described above, in the normal case you don’t have to remember this
initial value.