title: Dynamic form fields in Symfony 1.4
body: <p>I'm working on an e-commerce project and I got stuck at the cart update. Here I have to present a form using the contents of the current cart, with input fields containing the current quantities.</p>
  
  <p>I checked the documentation and the forums, but I didn't find anything useful. The problem is that i cannot declare the exact form fields in my form class because I don't know how many fields will be there. I tried this:</p>
  
  <pre><code>class CartForm extends sfForm {
    public function configure()
    {
      $cart = sfContext::getInstance()-&gt;getUser()-&gt;getShoppingCart();
      foreach ($cart-&gt;getItems() as $item) {
        $widgetName = $item-&gt;getId().'_quantity';
        $this-&gt;widgetSchema[$widgetName] = new sfWidgetFormInput(
          array(),
          array(
              'class' =&gt; 'quantity-input',
              'id'    =&gt; null,
              'name'  =&gt; $widgetName
          )
        );
        $this-&gt;widgetSchema-&gt;setDefault($widgetName, $item-&gt;getQuantity());
        $this-&gt;validatorSchema[$widgetName] = new sfValidatorInteger(array(
          'required' =&gt; true,
          'min'      =&gt; 1
        ),
        array());
      }
      unset($cart);
      $this-&gt;getWidgetSchema()-&gt;getFormFormatter()-&gt;setRowFormat('%field%%error%%hidden_fields%');
    }
  }
  </code></pre>
  
  <p>but I got some errors:</p>
  
  <pre><code>Fatal error: Cannot use object of type sfShoppingCart as array in /home/sfprojects/mdmall/lib/vendor/symfony/lib/form/sfForm.class.php on line 784
  </code></pre>
  
  <p>so this is not the right way. I tried to use raw  fields without any form classes (and validators) but something very odd happens, instead of getting the $_POST values i get a 404 error because when I submit the form it doesn't trigger this:</p>
  
  <pre><code>cart_update:
    url:    /cart/update.:sf_format
    class:  sfRequestRoute
    param:  { module: cart, action: update, sf_format: html }
    requirements: { sf_method: post }
  </code></pre>
  
  <p>If I remove the requirement, cart/update runs, but I dont have the $_POST data in the request object. Do you have any ideas?</p>