Symfony 1.4 中的动态表单字段
我正在开发一个电子商务项目,但我陷入了购物车更新的困境。在这里,我必须使用当前购物车的内容呈现一个表单,其中输入字段包含当前数量。
我检查了文档和论坛,但没有发现任何有用的东西。问题是我无法在表单类中声明确切的表单字段,因为我不知道会有多少字段。我尝试了这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些将帮助您动态添加表单字段并验证这些字段:
These will help you with regard to dynamically adding form fields and working with validation of those fields: