[Grok-dev] AddForm Mystery Number
Steve Schmechel
steveschmechel at yahoo.com
Mon Jun 1 19:22:57 EDT 2009
Steve Schmechel wrote:
>
>
> When looking at the view created by:
>
> class Upload(grok.AddForm):
> grok.context(FileContainer)
> form_fields =
> grok.AutoFields(zope.app.file.interfaces.IFile).select('data')
>
> you can see that the HTML for the submit button is generated like:
> <input type="submit" id="form.actions.4164642066696c65"
> name="form.actions.4164642066696c65" value="Add file" class="button" />
>
> Where does the "magic number" 4164642066696c65 come from?
>
>
I found the answer in the Action class of zope/formlib/form.py. (Seems
obvious now.) So for anyone who was curious, read below. I also have a
question (following the explanation) for anyone with time to offer me a
suggestion.
When a name is not passed to the constructor, the label value is checked
against a regex and, if it passes, it is lower-cased and used. If not, the
label is hex encoded and that value becomes the name used for the "id" and
"name" attributes.
Clever, if not entirely expected. In any case, it should be safe using this
value in a page template as long as the decorator on the view's add method,
@grok.action('Add file'), does not change.
However, I would prefer something a little more deterministic.
Is there a way to inject a name value into the grok.AutoFields when
"zope.app.file.interfaces.IFile" does not seem to support this?
----------
_identifier = re.compile('[A-Za-z][a-zA-Z0-9_]*$')
class Action(object):
interface.implements(interfaces.IAction)
def __init__(self, label, **options):
(success, failure, condition, validator,
prefix, name, data
) = _action_options(**options)
self.label = label
[self.success_handler, self.failure_handler,
self.condition, self.validator] = [
_callify(f) for f in (success, failure, condition, validator)]
if name is None:
if _identifier.match(label):
name = unicode(label).lower()
else:
name = label.encode('hex')
...
@namedtemplate.implementation(interfaces.IAction)
def render_submit_button(self):
if not self.available():
return ''
label = self.label
if isinstance(label, zope.i18nmessageid.Message):
label = zope.i18n.translate(self.label, context=self.form.request)
return ('<input type="submit" id="%s" name="%s" value="%s"'
' class="button" />' %
(self.__name__, self.__name__, label)
)
--
View this message in context: http://www.nabble.com/AddForm-Mystery-Number-tp23799424p23824030.html
Sent from the Grok mailing list archive at Nabble.com.
More information about the Grok-dev
mailing list