... apologies for the noise: this really isn't a TG question after all.
Just javascript/jquery jiggery-pokery...
For anyone who bumps into this, the .ajax call needs to be massaged
slightly
$.ajax({
....
data: {
basket_id: 1,
'fruit[]': [{name:'apples', qty: 2},
{name:'bananas', qty: 4},
{name:'oranges', qty: 6}
]
}
If the parameter name is followed by a '[]', formencode gets the message
and knows to treat it as a list...
Apologies again,
R
On Wednesday, 17 December 2014 13:19:00 UTC, Rob wrote:
>
> I'm trying to get a tg (v2.3.4) controller to deal with a json request
> with contains (as one of its parameters) a list of objects -- the idea
> being that (post validation), I have a variable 'fruit' (see below) as a
> list of anonymous hashes.
>
> I want to *avoid* simply passing a string and evaluating this using
> json.loads()
>
> I have a straightforward javascripy/jquery function in my template as
> follows:
>
> $.ajax({
> url: "tmp",
> type: 'POST',
> dataType: 'json',
> data: {
> basket_id: 1,
> fruit: [{name:'apples', qty: 2},
> {name:'bananas', qty: 4},
> {name:'oranges', qty: 6}
> ]
> }
> }).done(function(resp){
> console.log("I'm back...", resp)
> });
>
>
> I have a formencode Schema used for the validation of the controller's
> method (in two parts) as follows:
>
> from formencode import validators, Schema, ForEach
>
> class FruitSchema(Schema):
> name = validators.String()
> qty = validators.Int()
>
> class FruitbowlSchema(Schema):
> basket_id = validators.Int()
> fruit = ForEach(FruitSchema())
>
>
> My controller method is as follows:
>
> @expose('json')
> @validate(validators=FruitbowlSchema())
> def tmp(self, basket_id, fruit):
> log.debug("basket_id: %s", basket_id)
> log.debug("fruit: %s", fruit)
> return dict()
>
>
> Calling tmp() from the $.ajax I see that (prior to validation) the
> following parameters are included in the request:
>
> basket_id=u'1' <type 'unicode'>
> fruit[0][qty]=u'2' <type 'unicode'>
> fruit[0][name]=u'apples' <type 'unicode'>
> fruit[1][qty]=u'4' <type 'unicode'>
> fruit[1][name]=u'bananas' <type 'unicode'>
> fruit[2][name]=u'oranges' <type 'unicode'>
> fruit[2][qty]=u'6' <type 'unicode'>
>
> However, the jquery call is complaining of a 500 error and Turbogears'
> debug gives me:
> TypeError: tmp() got an unexpected keyword argument 'fruit[2][name]'
>
>
> My understanding is that @validate with formencode can deal with the
> single 'fruit' parameter in the controller's method
>
> And I also have a strong suspicion that I've (successfully) accomplished
> this before -- -- but can't remember how! -- clearly I'm doing something
> wrong here!!
>
> Can anyone help me out... ?
>
> Many thanks,
> Rob
>
>
>
>
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.