Ok. Here is one way to go about it that uses the ForEach validator in
a formencode schema to ensure that a list (and not a single value) is
returned when the multiple select fields is posted to the submit
action.

Say you have a multiple select field called "users" somewhere in your
form.

You can define a validator in your schema for the users field as
follows :-

import formencode
from formencode import htmlfill, validators
from formencode.foreach import ForEach

class UserFormSchema(formencode.Schema):
    allow_extra_fields = True
    filter_extra_fields = True
   users = ForEach(validators.Int())
.........

Then in the controllers submit action for your form - lets call this
save, you can access the submitted form value for the users field as
follows :-

    def save(self, id=None):
        schema = UserFormSchema()
        try:
            c.form_result = schema.to_python(dict(request.params.mixed
()))
        except formencode.Invalid, error: #validation failed
            c.form_result = error.value
            c.form_errors = error.error_dict or {}
            #repopulate the original form with the currently selected
values/errors and render again
        else: #validation suceeded
            users = kw['users'] # this is a list of integers
corresponding to the selection
            # take action required to save 'users' and all other form
fields to the data base

Note that each of the integers in the list corresponds to a user
selected in the multiple select field.

I.e. if the constructed selected field is of the form ....

<select name=users multiple >
          <option value='1' >User 1</option>
          <option value='2' >User 2</option>
          <option value='3' >User 3</option>
          <option value='4' >User 4</option>
          <option value='5' >User 5</option>
        </select>

and the User selects Users 3 and 5, the value returned by the ForEach
validator will be [3,5] and this will be available when accessed in
the try/else clause thus :-

users = kw['users']

For more information on how to use this pattern (manual validation),
please see the following URL:-

http://pylonsbook.com/en/1.0/working-with-forms-and-validators.html#introducing-formencode


On Jul 13, 12:44 pm, Naveen Kumar Reddy Nandipati
<[email protected]> wrote:
> Yes you are right I am planning to access the selected users from a
> form and send them to submit button action to delete the users by
> implementing the function of delete users in the controller. So I am
> unable to access the multiple selected users from the form template to
> the action controller. Can you help me how to pass a list of users
> from the form template to the controller action.
>
> On Mon, Jul 13, 2009 at 4:23 AM, afrotypa<[email protected]> wrote:
>
> > So are you trying to access the multiple selected values from a form
> > submission inside the submit action of your controller? This sounds
> > like what you are asking (or at least a part of it). Please clarify
> > your question. Setting global variables in the template for this
> > purpose likely will not work.
>
> > On Jul 13, 3:56 am, kumar <[email protected]> wrote:
> >> I tried to pass variables between templates and controllers by using
> >> global. But when I set the globals in template the function that is
> >> supposed to be work doesn't work. So i am unable to set the global
> >> variable in template. The thing is actually I want to implement the
> >> delete users function from the user table displayed on the web portal.
> >> it is working fine for one user. But when I select multiple users i
> >> need to pass the selected users list to the controller and implement
> >> the delete user function. I am struck here can anyone help me out.
--~--~---------~--~----~------------~-------~--~----~
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