Symfony 1.4 中的动态表单字段

发布于 2024-09-29 19:42:01 字数 1564 浏览 0 评论 0原文

我正在开发一个电子商务项目,但我陷入了购物车更新的困境。在这里,我必须使用当前购物车的内容呈现一个表单,其中输入字段包含当前数量。

我检查了文档和论坛,但没有发现任何有用的东西。问题是我无法在表单类中声明确切的表单字段,因为我不知道会有多少字段。我尝试了这个:

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%');
  }
}

但我遇到了一些错误:

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

所以这不是正确的方法。我尝试使用没有任何表单类(和验证器)的原始字段,但发生了一些非常奇怪的事情,我没有获取 $_POST 值,而是收到 404 错误,因为当我提交表单时,它不会触发此错误:

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

如果我删除要求,购物车/更新运行,但我在请求对象中没有 $_POST 数据。你有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2024-10-06 19:42:01

这些将帮助您动态添加表单字段并验证这些字段:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文