Issue #526 resolved

Splitting request.params to create arrays

Anonymous created an issue

Request Parameters can be rearranged, to provide easy array access, this is in order to organize big forms and avoid having to create a function with several arguments.

Example: {{{ def form(): return ''' <form method="post" action="save"> First Group:<br /> <input type="text" name="" />Name<br /> <input type="text" name="" />Info<br /> Second Group:<br /> <input type="text" name="" />Name<br /> <input type="text" name="" />Info<br /> <button type="submit">Accept</button> </form>'''

def save(first, second): return '%s, %s"%(first, second) }}}

The method I added to the cherrypy/ Request class: {{{ def update_params(self): for paramkey in self.params.keys(): split = paramkey.split('.') if len(split)<2: continue current = self.params for key in split[0:-1]: if not current.has_key(key): current[key] = {} current = current[key] current[split[-1]] = self.params[paramkey] del self.params[paramkey] }}}

Reported by sduran

Comments (7)

  1. guest

    It fails with a TypeError: object does not support item assignment.

    Which should be expected, I don't think it's a good idea to create two variables (of different types) with the same name unless you're passing that variable as an array of values.

  2. michele

    This one seems like a very good candidate for a tool inside a cherrypy-extras package, I really don't think it belongs to CP itself so I'm closing it.

    It should work the way we have done it in TurboGears (a filter), the one we've used there is using FormEncode NestedVariables.

    I think it's worth using a custom tool that doesn't depend on FE and that uses a different schema.

    For example:

    should give you a params like this one:

    {'ticket': [{'type': 'ticket 0 type', 'summary': 'ticket 0 summary'}, {'type': 'ticket 1 type', 'summary': 'ticket 1 summary'}], 'user': {'name': 'michele', 'email': ''}}

    it's very useful to manage complex forms (like this one trac uses for tickets) and repeating fields.

  3. Log in to comment