doumeng3345 2010-10-28 20:38
浏览 54
已采纳

Symfony 1.4中的动态表单字段

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?

  • 写回答

1条回答 默认 最新

  • dqrfdl5708 2010-10-28 20:46
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装