Php symfony2创建没有实体类的表单
使用Symfony2.3.4 我试图在不使用类型的情况下创建一个表单,它实际上是一个非常小的表单,只有两个选择从数据库加载它们的选项,到目前为止,它工作正常,我不能做的是在提交表单时获取表单数据(在控制器中)。我试着按照指示去做,但做不好 以下是我目前的代码: 控制器: 函数将数据传递到表单:Php symfony2创建没有实体类的表单,php,forms,symfony,Php,Forms,Symfony,使用Symfony2.3.4 我试图在不使用类型的情况下创建一个表单,它实际上是一个非常小的表单,只有两个选择从数据库加载它们的选项,到目前为止,它工作正常,我不能做的是在提交表单时获取表单数据(在控制器中)。我试着按照指示去做,但做不好 以下是我目前的代码: 控制器: 函数将数据传递到表单: public function selectAction($id, $id_actpost){ $em = $this->getDoctrine()->getManager()
public function selectAction($id, $id_actpost){
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ActPostBundle:Edition')->find($id);
$students = $em->getRepository('PersonBundle:Students')->findAll();
$students2 = $em->getRepository('PersonBundle:ForeignStudents')->findAll();
if (!$entity) {
throw $this->createNotFoundException('Unable to find Edicion entity.');
}
return $this->render('ActPostBundle:Edition:select.html.twig', array(
'entity' => $entity,
'id_actpost' => $id_actpost,
'students' => $students,
'foreignstudents' => $students2,
));
}
public function update_selectedAction(Request $request, $id, $id_actpost) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ActPostBundle:Edition')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Edicion entity.');
}
$defaultData = array('message' => 'Type here');
$editForm = $this->createFormBuilder($defaultData)
->add('students','choice')
->add('students2', 'choice')
->getForm();
$editForm->handleRequest($request);
关于表单本身的html片段:
<form class="form-horizontal sf_admin_form_area"
action="{{ path('edition_update_selected',
{ 'id': entity.id, 'id_actpost': id_actpost }) }}"
method="post" enctype="multipart/form-data">
<div style="margin-left: 80px" class="row-fluid">
<div class="span12">
<select name="students" multiple="multiple">
{% for s in students %}
<option {%if s in entity.students%}selected="selected"{%endif%}>
{{s}}</option>
{%endfor%}
</select>
</div>
</div>
<div class="row-fluid">
<select name="students2" multiple="multiple">
{% for s in students2 %}
<option {%if s in entity.foreignstudents%}selected="selected"
{%endif%}>{{s}}</option>
{%endfor%}
</select>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">
<i class="glyphicon-refresh"></i> {{'Update' | trans}}</button>
<a class="btn" href="{{ path('edition', {'id_actpost' : id_actpost }) }}">
<i class="glyphicon-ban-circle"></i> {{'Cancel' | trans }}
</a>
</div>
</form>
我想知道我所读的内容是否真的是我所需要的,因为尽管我认为我可能是错的,但任何关于这件事的见解,甚至任何其他方法都将非常感激。你应该使用symfony的表单生成器在你的
更新\u selectedAction()
中构建表单
public function update_selectedAction(Request $request, $id, $id_actpost)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ActPostBundle:Edition')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Edicion entity.');
}
$defaultData = array('message' => 'Type here');
$form = $this->createFormBuilder($defaultData)
->add('students', 'entity',array('class' => 'PersonBundle:Students',
'property' => 'students','expanded'=>false,'multiple'=>false))
->add('students2', 'entity',array('class' => 'PersonBundle:ForeignStudents',
'property' => 'foreignstudents','expanded'=>false,'multiple'=>false))
->add('submit','submit')
->getForm();
if ($request->getMethod() == "POST") {
$form->submit($request);
if ($form->isValid()) {
$postData = current($request->request->all());
var_dump($postData); /* All post data is here */
/* echo $postData['students']; */
/* echo $postData['students2']; */
/*
* do you update stuff here
* */
}
}
return $this->render('ActPostBundle:Edition:select.html.twig', array('form'=>$form->createView()));
}
在您的twig中,即ActPostBundle:Edition:select.html.twig
呈现您的表单
{{ form(form) }}
根据评论编辑
在细枝文件中,将窗体渲染为
{{ form_errors(form) }}
{{ form_row(form.students) }}
{{ form_row(form.students2) }}
{{ form_row (form._token) }}
<input type="submit"> /* your submit button */
但是如果我这样做的话,我也必须修改selectAction,对吗?因为实际上,它并没有将表单变量发送到.twig。记住,selectAction是将当前信息加载到表单中,并更新selectedAction以获取提交的数据,两者都必须与.twig交互。使用此方法后,您不需要使用
selectAction
,您可以直接使用update\u selectedAction
构建表单,也可以保存表单,我只是照你说的做了,现在问题是$request->getMethod()==“POST”不正确,所以我尝试了$editForm->submit($request);前面没有if,然后是var_dump($editForm->getErrors());模具();为了检查错误,我得到了这个带有一些属性的错误对象,其中一个是:“message'=>”CSRF令牌无效。请尝试重新提交表单。'敲响任何警钟…?@jmiguel您是如何通过表单(表单)
呈现表单的?或者放置单个字段如果你正在渲染单个字段,那么你还需要{{form_row(form.\u token)}
来渲染你得到了它,非常感谢,学到了很多,非常有用!
$students = $em->getRepository('PersonBundle:Students')->findAll();
$students2 = $em->getRepository('PersonBundle:ForeignStudents')->findAll();
$studentsArray=array();
$students2Array=array();
foreach($students as $s){
$studentsArray[$s->getStudents()]=$s->getStudents();
}
foreach($students2 as $s){
$students2Array[$s->getForeignstudents()]=$s->getForeignstudents();
/* here array key part will be the value of selectbox like $students2Array['your val to get in post data'] */
}
$form = $this->createFormBuilder($defaultData)
->add('students', 'choice',array('choices' => $studentsArray,'expanded'=>false,'multiple'=>false))
->add('students2', 'choice',array('choices' => $students2Array,'expanded'=>false,'multiple'=>false))
->add('submit','submit')
->getForm();