.reset_values() for numbers in forms

6 views
Skip to first unread message

Terrence Brannon

unread,
May 6, 2015, 6:29:15 PM5/6/15
to nagare...@googlegroups.com
In the Nagare Form example:

The age is set to an integer value of 20. 

Curiously, the .reset_values() method:

does not have a line like
    self.age(20)
or
    self.age('20')


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.

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.

But we still have the issue of whether numbers should be initialized
in the properties as numbers or strings.
   

Alain Poirier

unread,
May 10, 2015, 10:58:00 AM5/10/15
to nagare...@googlegroups.com
> Le 7 mai 2015 à 00:29, Terrence Brannon <meta...@gmail.com> a écrit :
>
> In the Nagare Form example:
> http://www.nagare.org/demo/form
>
> The age is set to an integer value of 20.
>
> Curiously, the .reset_values() method:
> http://www.nagare.org/trac/browser/examples/nagare/examples/form.py#L101
>
> does not have a line like
> self.age(20)
> or
> self.age('20’)

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.
Reply all
Reply to author
Forward
0 new messages