[Grok-dev] AddForm Mystery Number
steveschmechel at yahoo.com
Mon Jun 1 23:09:58 EDT 2009
So my real mistake was introducing whitespace into the "label" parameter of @grok.action.
I understand why the whitespace would be a problem, but why isn't it simply removed instead of falling all the way back to ugly hex encoding?
Oh well, not a Grok problem.
I guess I am back to wondering if it is good practice to call an auto-generated form view from another template. I could try to link the names together with a global constant in my code and expose it as a variable to my template.
Is there a more elegant way to inject an auto-generated AddForm into a portion of a Grok view?
I'm sure there is a whole world of viewlets and such about to be recommended to me. :-)
More simply, I'll ask again:
- Does anyone really use auto-generated forms as a basis for real world projects, or are they just cool tools to use in demos and example code?
- Should I just figure that any serious development requires a complete set of some flavor of custom page templates for each view/form?
--- On Mon, 6/1/09, Steve Schmechel <steveschmechel at yahoo.com> wrote:
> From: Steve Schmechel <steveschmechel at yahoo.com>
> Subject: Re: [Grok-dev] AddForm Mystery Number
> To: grok-dev at zope.org
> Date: Monday, June 1, 2009, 6:22 PM
> Steve Schmechel wrote:
> > When looking at the view created by:
> > class Upload(grok.AddForm):
> > grok.context(FileContainer)
> > form_fields =
> > you can see that the HTML for the submit button is
> generated like:
> > <input type="submit"
> > name="form.actions.4164642066696c65" value="Add file"
> class="button" />
> > Where does the "magic number" 4164642066696c65 come
> 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
> 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
> Is there a way to inject a name value into the
> grok.AutoFields when
> "zope.app.file.interfaces.IFile" does not seem to support
> _identifier = re.compile('[A-Za-z][a-zA-Z0-9_]*$')
> class Action(object):
> def __init__(self, label, **options):
> (success, failure, condition,
> prefix, name, data
> ) =
> self.label = label
> self.validator] = [
> _callify(f) for f
> in (success, failure, condition, validator)]
> if name is None:
> name = unicode(label).lower()
> name = label.encode('hex')
> def render_submit_button(self):
> if not self.available():
> return ''
> label = self.label
> if isinstance(label,
> label =
> zope.i18n.translate(self.label, context=self.form.request)
> return ('<input type="submit" id="%s"
> name="%s" value="%s"'
> ' class="button"
> />' %
> 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.
> Grok-dev mailing list
> Grok-dev at zope.org
More information about the Grok-dev