我問了很多,然後問(永遠不夠),所以我希望這不是一個重複的問題。提交數組類型原則表格
那麼這裏我去:
我開發用symfony(使用FOSRestBundle和JMSSerializerBundle)一個REST API。我儘量讓與後續的身體POST查詢:
{
"name":"Planta 1",
"parentJoint": null,
"owner": 1,
"ownerCRUD":{
"C":"0",
"R":"0",
"U":"0",
"D":"0"
}
爲了填補遵循原則的實體
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Joint
*
* @ORM\Table(name="joint")
* @ORM\Entity(repositoryClass="AppBundle\Repository\JointRepository")
*/
class Joint
{
(...)
/**
* @var array
*
* @ORM\Column(name="owner_crud", type="array",nullable = false)
*/
private $ownerCRUD;
/**
* Set ownerCRUD
*
* @param array $ownerCRUD
* @return Joint
*/
public function setOwnerCRUD($ownerCRUD)
{
$this->ownerCRUD = $ownerCRUD;
return $this;
}
/**
* Get ownerCRUD
*
* @return array
*/
public function getOwnerCRUD()
{
return $this->ownerCRUD;
}
public function __toString()
{
return 'any string';
}
}
我試圖通過這個控制器的功能,使之:
/**
* Description: This method create and persist in database a new Joint Element
*
* @ApiDoc(
*)
*
* @var Request $request
*/
public function postJointAction(Request $request)
{
try {
$parameters = $request->request->all();
dump($parameters);
$joint = new Joint();
$form = $this->createForm('AppBundle\Form\JointType', $joint);
$form->submit($parameters, 'POST');
dump($form);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$joint = $form->getData();
$em->persist($joint);
$em->flush($joint);
$joint = $em->getRepository('AppBundle:Joint')->find($joint->getId());
//Create a View
$templateData = array('joint' => $joint);
$view = $this->view($templateData,200)
->setTemplate("joint/show.html.twig")
->setTemplateVar('joint')
->setTemplateData($templateData)
->setData($templateData)
;
return $this->handleView($view);
}
} catch (InvalidFormException $exception) {
return $exception->getForm();
}
}
而且我的形式是這樣的:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class JointType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('parentJoint')
->add('owner')
->add('ownerCRUD') ;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Joint',
'csrf_protection' => false
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_joint';
}
/**
* @return string
*/
public function getName()
{
return "";
}
}
的問題是,形式不正確地得到ownerCRUD領域,形式假定有額外的字段,而不是正確的字段提前
This form should not contain extra fields. ownerCRUD
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[ownerCRUD] = [C => 0, R => 0, U => 0, D => 0]
感謝
我的樹枝文件
{% extends 'base.html.twig' %}
{% block body %}
<h1>Joint</h1>
<table>
<tbody>
<tr>
<th>Id</th>
<td>{{ joint.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ joint.name }}</td>
</tr>
</tbody>
</table>
<ul>
<li>
<a href="{{ path('get_joints') }}">Back to the list</a>
</li>
<li>
<a href="{{ path('put_joint', { 'joint': joint.id }) }}">Edit</a>
</li>
</ul>
{% endblock %}
能否請您告訴我們'twig'文件的形式? –
將字段名稱設置爲'「」'似乎非常無效: - /您可以提交'['formName'=> $ parameters]'? –
我把我的樹枝文件,但我認爲這不是問題,我只有在數據存儲以報告成功時才使用它 –