symfony 形式:在客户端设置实体
symfony form: set entity at client side
我是 symfony2 的新手,所以我可能不知道使用表单时的一些重要细节...
如何在页面呈现后在客户端为表单设置实体?
在我的页面上,我有几个产品和一个表格。我需要通过单击选择要编辑的产品。所以我需要在客户端修改表单数据。这是我的简化模板代码:
<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>
但是当我尝试提交它时,我得到一个错误:"Neither the property "id" nor one of the methods "addId()"/"removeId()", "setId()", "id()"、“__set()”或“__call()”在 class "AppBundle\Entity\Product"
中存在并具有 public 访问权限
任何帮助将不胜感激。
好吧,我至少找到了解决方案。不确定它是否是最干净的,但它确实有效。要在客户端动态选择要编辑的对象,您需要执行以下简单步骤:
- 在表单中创建一个隐藏字段,该字段将保留您当前选择进行编辑的对象的 ID。
- 使用javascript.
在客户端单击(或任何你喜欢的)将对象的ID保存到这个字段中
表单提交后,使用此字段中的 ID 从数据库中获取所需的对象并对其应用更改。
这是一个控制器示例:
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>
我是 symfony2 的新手,所以我可能不知道使用表单时的一些重要细节... 如何在页面呈现后在客户端为表单设置实体?
在我的页面上,我有几个产品和一个表格。我需要通过单击选择要编辑的产品。所以我需要在客户端修改表单数据。这是我的简化模板代码:
<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>
但是当我尝试提交它时,我得到一个错误:"Neither the property "id" nor one of the methods "addId()"/"removeId()", "setId()", "id()"、“__set()”或“__call()”在 class "AppBundle\Entity\Product"
中存在并具有 public 访问权限任何帮助将不胜感激。
好吧,我至少找到了解决方案。不确定它是否是最干净的,但它确实有效。要在客户端动态选择要编辑的对象,您需要执行以下简单步骤:
- 在表单中创建一个隐藏字段,该字段将保留您当前选择进行编辑的对象的 ID。
- 使用javascript. 在客户端单击(或任何你喜欢的)将对象的ID保存到这个字段中
表单提交后,使用此字段中的 ID 从数据库中获取所需的对象并对其应用更改。 这是一个控制器示例:
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>