2
votes

I do have a form. This form submits for example 3 words (beer, coke, wine). In the next action I do want to have a three choice widgets with one or more choices:

-beer:                //first choice field
  * buddy lighty      //choice one
  * busch             //choice two
  * miler             //choice three

-coke:                //second choice field
  * coke diet
  * coke
  * coke vanilla

-wine:                //third choice field
  * bordeaux
  * suave
  * champange

<submit-button>

I want every choice in one action. So if somebodey make a choice busch, coke, suave will be submittet. How can I realise it?

Update:

Thanks for the comment. I might forget to say that I don't know how many dropdown menus I need. There might be just beer and coke or beer, coke, wine and juice. It depends from what the user fill out the number of forms the action before! I tried to do it with a foreach-loop in forms.class.php. But it doesn't help.

I use Doctrine.

1
Just add the 3 different dropdowns to one and the same form? Could you at least post what you tried before and where you failed, because you seem to be asking very basic stuff (which is well documented by the way). - Gerry
I did an update on my question. For me it is not a basic problem. Sorry. Do might have code for me? - craphunter
Is this a Doctrine object form? - Gerry
Yes it is Doctrine. Can you help me? - craphunter
When you say "In the next action..." you mean the user will submit the form several times? Do you know beforehand the kind of dropdowns are possible the user to choose from? - P. R. Ribeiro

1 Answers

0
votes

One simple way to do this (depends on your model, too) is to configure each item as nullable, and then use form options to show/hide certain widgets. e.g., if your schema looks like this lazy example:

DrinkOrder:
  columns:

    # ...

    beer:
      type: enum
      values: [Old Peculier,Tribute,Deuchars]
      notnull: false

     wine:
         type: enum
         values: [Bordeaux,Suave,Champagne]
         notnull: false

    # ...etc

Configure your form like this:

class DrinkOrderForm extends BaseDrinkOrderForm
{
    public function configure()
    {
        if ($this->getOption('hide_wine'))
        {
            $this->widgetSchema['wine'] = new sfWidgetFormInputHidden;
        }

        // … etc
    }
}

And then when the action of the previous form submits you can pass options to the form, like:

$this->form = new DrinkOrderForm($drink_order, array(
    'hide_wine' => true,
    'hide_beer' => false,
));

This is just a quick example - instead of an ENUM type, you could use relations to another table (e.g. wine_id and an sfWidgetFormDoctrineChoice widget & validator).

One thing you can't do is have 3-4 separate forms, because web browsers will only submit one of them. You either have to embed forms within each other, or use the simpler technique above, depending on how your model's set up.

If the number of types of choice isn't fixed, then you'd want to look into using something like the form system's embedRelation method (or the ahDoctrineEasyEmbeddedRelationsPlugin) to dynamically add sub-forms. It's hard to know from your example just how far you want to go. :)