views:

27

answers:

1

Hi there,

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.

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:

class CartForm extends sfForm {
  public function configure()
  {
    $cart = sfContext::getInstance()->getUser()->getShoppingCart();
    foreach ($cart->getItems() as $item) {
      $widgetName = $item->getId().'_quantity';
      $this->widgetSchema[$widgetName] = new sfWidgetFormInput(
        array(),
        array(
            'class' => 'quantity-input',
            'id'    => null,
            'name'  => $widgetName
        )
      );
      $this->widgetSchema->setDefault($widgetName, $item->getQuantity());
      $this->validatorSchema[$widgetName] = new sfValidatorInteger(array(
        'required' => true,
        'min'      => 1
      ),
      array());
    }
    unset($cart);
    $this->getWidgetSchema()->getFormFormatter()->setRowFormat('%field%%error%%hidden_fields%');
  }
}

but I got some errors:

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

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:

cart_update:
  url:    /cart/update.:sf_format
  class:  sfRequestRoute
  param:  { module: cart, action: update, sf_format: html }
  requirements: { sf_method: post }

If I remove the requirement, cart/update runs, but I dont have the $_POST data in the request object. Do you have any ideas?

A: 

These will help you with regard to dynamically adding form fields and working with validation of those fields:

prodigitalson
Great thanks for your response!!
edem