Javascript symfony表单:在客户端设置实体
我对symfony2非常陌生,所以在使用表单时,我可能不知道一些重要的细节。。。 在页面呈现之后,如何在客户端为表单设置实体 在我的页面上,我有几个产品和一个表单。我需要通过单击选择要编辑的产品。所以我需要修改客户端的表单数据。以下是我的简化模板代码:Javascript symfony表单:在客户端设置实体,javascript,forms,symfony,submit,Javascript,Forms,Symfony,Submit,我对symfony2非常陌生,所以在使用表单时,我可能不知道一些重要的细节。。。 在页面呈现之后,如何在客户端为表单设置实体 在我的页面上,我有几个产品和一个表单。我需要通过单击选择要编辑的产品。所以我需要修改客户端的表单数据。以下是我的简化模板代码: <div class="product" onclick="editFormProduct({ id: '{{ product1.id }}', name: '{{ product1.name }}'
<div class="product" onclick="editFormProduct({
id: '{{ product1.id }}',
name: '{{ product1.name }}'
price: '{{ product1.price }}'
})">
<span>{{ product1.name }}</span>
<span>{{ product1.price }}</span>
</div>
<div class="product" onclick="editFormProduct({
id: '{{ product2.id }}',
name: '{{ product2.name }}'
price: '{{ product2.price }}'
})">
<span>{{ product2.name }}</span>
<span>{{ product2.price }}</span>
</div>
{{ form_start(form, {'attr': {'id': 'form-product'}}) }}
{{ form_widget(form.name) }}
{{ form_widget(form.price) }}
{{ form_end(form) }}
<script type="text/javascript">
function editFormProduct(product) {
$('#form-product').find('input[id=form_id]').val(product.id);
$('#form-product').find('div[name=name] input').val(product.name);
$('#form-product').find('div[name=price] input').val(product.price);
}
</script>
但是当我尝试提交它时,我得到了一个错误:属性id或addId/removeId、setId、id、\uuuu set或\uuu调用中的一个方法都不存在,并且在类AppBundle\Entity\Product中具有公共访问权限
任何帮助都将不胜感激。好吧,至少我已经找到了解决办法。不确定它是否是最干净的,但它能工作。要在客户端选择要动态编辑的对象,您需要执行以下简单步骤: 在表单中创建一个隐藏字段,该字段将保留您当前选择要编辑的对象的id。 通过单击或在客户端使用javascript将对象的id保存到此字段中。 表单提交后,使用此字段中的id从DB中获取所需的对象并对其应用更改。 以下是控制器示例:
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
//when the form is submitted, get edit_id
//probably there is a better way to get the form data
//before creating the $form object...
if ($request->request->get('form') != null)
$editedProduct = $repository->find($request->request->get('form')['edit_id'] - 1);
else
$editedProduct = new Product();
$form = $this->createFormBuilder($editedProduct)
->add('name', 'text')
->add('price', 'text')
//hidden field that will store the id of the edited object
//must not to be mapped, as it doesn't belong to the entity
->add('edit_id', 'hidden', array('mapped' => false));
$form->handleRequest($request);
...
}
}
并且对应的模板样本与原始样本几乎相同:
<div class="product" onclick="editFormProduct({
id: '{{ product1.id }}',
name: '{{ product1.name }}'
price: '{{ product1.price }}'
})">
<span>{{ product1.name }}</span>
<span>{{ product1.price }}</span>
</div>
<div class="product" onclick="editFormProduct({
id: '{{ product2.id }}',
name: '{{ product2.name }}'
price: '{{ product2.price }}'
})">
<span>{{ product2.name }}</span>
<span>{{ product2.price }}</span>
</div>
{{ form_start(form, {'attr': {'id': 'form-product'}}) }}
{{ form_widget(form.name) }}
{{ form_widget(form.price) }}
{{ form_end(form) }}
<script type="text/javascript">
function editFormProduct(product) {
//fill the edit_id field with the chosen product id
$('#form-product').find('#form_edit_id').val(product.id);
$('#form-product').find('#form_name').val(product.name);
$('#form-product').find('#form_price').val(product.price);
}
</script>
请通过控制器功能,我想你使用$em->persist!!!Mohammad,只需使用$em->flush来更新dataSorry,但我询问了如何更改绑定到表单的数据。我需要在页面呈现后动态更改表单数据。我已经改变了我的问题,所以现在应该更清楚了。隐藏字段add上的数组'mapped'=>false解决了我的问题。非常感谢。