On Sep 4, 2006, at 5:23 PM, Fernando Aires wrote:
>    I need to use a subform inside a form. For example: if I'm
> classifying a football team, I need to add all player's name and role
> (goalie, middlefielder etc.), and also pass it as form info.
>    I see that Oprius Software uses something alike, but I couldn't
> find its source code. Could someone be able to post a sample, or
> explain how can I do it using TurboGears?

A form inside a form?? Can you really do that? As Jason pointed out,  
you can use a RepeatingFieldSet for this. Something like:

class PlayerFields(WidgetsList):
        id = HiddenField(validator=Int(if_empty=None))
        name = TextField()
        role = SingleSelectField(options=get_roles)

class TeamFields(WidgetsList):
        name = TextField()
        location = TextField()
        players = RepeatingFieldSet(fields=PlayerFields())

team_form = TableForm(fields=TeamFields())

# Note, "team" is assumed to be a Team SO instance passed by
# the "default" method of fastdata.DataController in a RESTful way.
# team.players is a MultipleJoin (or RelatedJoin if your players are  
over-exploited ;) )
# to Player
@expose(...)
def edit_team(self, team):
        # let the user edit the current players and up to 3 more in one go
        repetitions = len(team.players) + 3
        return dict(
                form = team_form,
                form_params = dict(
                        value = team,
                        repetitions = dict(
                                players = repetitions
                        ),
                        action = "update_team",
                )
        )


@error_handler(edit_team)
@validate(form=team_form)
@expose()
def update_team(self, team, name, location, players):
        team.set(name=name, location=location)
        for player in players:
                id = player.pop(id)
                if id:
                        player = Player.get(id)
                        player.set(**player)
                elif player['name'] or player['role']:
                        # if no id is set but name or role is the user is 
creating a
                        # new player
                        new = Player(**player)
                        # Not sure SO allows this, I already converted to SA... 
;)
                        team.players.append(new)
        flash("Team updated successfully")
        raise redirect("../")

Disclaimer: Just wrote this on Mail. Take it as pseudo-code. It'll  
probably won't work without tweaking or might not
work at all ;) However, you can get an idea.... (I've written similar  
working-code but I'm now too lazy to look it up, sorry). Play around  
and if you get stuck just ask :)

HTH,
Alberto

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to