Take a look at Formencode.foreach.ForEach.  According to the docs
(http://formencode.org/Validator.html) you can use it to define
repeating forms or (more conventionally) repeating groups within a
form.

In the latter case you will want this at the top of your form schema:

    pre_validators = [variabledecode.NestedVariables()]

I do this indirectly, by having my application's form schemas all
inherit from a base schema which is defined (in my lib/base.py) )as
follows:

    class BaseSchema(formencode.Schema):
        """
        Base form schema
        """
        allow_extra_fields = True
        filter_extra_fields = True
        pre_validators = [variabledecode.NestedVariables()]

I don't know if/how NestedVariables applies if you don't have an
enclosing form for your repeating group - I've never tried it.  A top
level dict for your json payload would be more extensible (and safer?)
than an array though.

Regards,
Mike

On May 16, 7:21 pm, DD <[email protected]> wrote:
> Hi,
> I have some that submits an array of strings via ajax to be saved on the
> server.
> Basically, I need to get this same array of strings to be saved in the
> order it was passed.
>
> *Below is a snippet from the JS code:
> *
> function saveArray() {
>      new_order = ["a", "b", "c", "d"];
>
>      var fdata={};
>      fdata.new_order = new_order;
>
>      $.ajax({
>          url: '/mycontroller/save',
>          type:"POST",
>          dataType: 'json',
>          data:fdata
>          });
>      return false;
>
> }
>
> *Controller:*
>
>      class OrderForm(formencode.Schema):
>          new_order = formencode.ForEach(String)
>          ## other fields
>
>     �...@validate(schema=OrderForm(), form='ajax_error', variable_decode=True)
>      def save(self):
>          log.debug("All params: %s"%request.params)
>          new_order=self.form_result.get('new_order')
>          log.debug("New_order %s"%new_order)
>
>          # do something with new_order..
>
> When the request is submitted, the request params received by controller
> are:
>
> request.params:
> UnicodeMultiDict([('new_order[]', u'a'), ('new_order[]', u'b'),
> ('new_order[]', u'c'), ('new_order[]', u'd')])
>
> and
>
> self.form_result['new_order'] is an empty array
>
> What I ideally want to have a single array new_order with all the values
> in this array.
>
> Any suggestions here?
> Thanks,
> DD.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/pylons-discuss?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to