【发布时间】:2018-03-05 00:50:32
【问题描述】:
我有一个控制器操作方法,它应该处理一个两拆分的表单。每个表单只处理我的实体Workflow 的几个属性。提交第一个表单后,我可以毫无问题地创建和渲染第二个表单。现在的问题:
提交第二个表单后,第一个表单中设置的所有值的信息都没有了,这意味着当调用submit(或handleRequest在这里没有任何区别)时,实体对象只保存属性的数据以第一种形式设置,它甚至无法正确解析某些值。
这是控制器(带有一些 cmets):
public function createWorkflowAction(Request $request, Project $project, Workflow $workflow = null) {
if(!$workflow) {
$workflow = new Workflow($project);
}
$firstFormPart = $this->createForm(WorkflowStatesType::class, $workflow);
// $firstFormPart->handleRequest($request);
$firstFormPart->submit($request->get($firstFormPart->getName()), false);
$secondFormPart = $this->createForm(WorkflowTransitionsType::class, $workflow);
// secondFormPart is created correct with all values after submitting $firstFormPart and calling submit
if($firstFormPart->isSubmitted() && $firstFormPart->isValid()) {
return $this->render('@MyBundle/Workflow/workflow_edit_create_second_part.html.twig', array(
'form' => $secondFormPart->createView(),
));
// This will render correctly with all values submitted in the $firstFormPart
}
$secondFormPart->submit($request->get($secondFormPart->getName()), false);
// $secondFormPart->handleRequest($request);
// HERE IS THE PROBLEM -> After submitting the $secondFormPart all property values set in the $firstFormPart are gone
if($secondFormPart->isSubmitted() && $secondFormPart->isValid()) {
dump($workflow);
die();
}
return $this->render('@MyBundle/Workflow/workflow_edit_create_first_part.html.twig', array(
'form' => $firstFormPart->createView(),
));
}
WorkflowStatesType:
class WorkflowStatesType extends AbstractType {
/**
* @var \Doctrine\ORM\Mapping\ClassMetadata
*/
private $classMetadata;
/**
* WorkflowType constructor.
* @param EntityManager $em
*/
public function __construct(EntityManager $em) {
$this->classMetadata = $em->getClassMetadata(Workflow::class);
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->setMethod('PATCH')
->add('name', TextType::class, array(
'label' => 'nameTrans',
'attr' => array('maxLength' => $this->classMetadata->getFieldMapping('name')['length']),
))
->add('states', CollectionType::class, array(
'entry_type' => StateType::class,
'allow_add' => true,
'error_bubbling' => false,
'by_reference' => false,
'label' => 'workflowStatesTrans',
))
->add('next', SubmitType::class, array(
'label' => 'nextFormPartTrans',
));
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => Workflow::class,
'translation_domain' => 'My_Bundle',
));
}
}
WorkflowTransitionsType:
class WorkflowTransitionsType extends AbstractType {
/**
* @var Workflow
*/
private $workflow;
/**
* @var Session
*/
private $session;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
/** @var Workflow $workflow */
$this->workflow = $options['data'];
$builder
->setMethod('PATCH')
->add('initialState', ChoiceType::class, array(
'choices' => $this->workflow->getStates(),
'choice_label' => function($state) {
return ($state && $state instanceof State) ? $state->getStatekey() : 'noVal';
},
'choice_value' => function($state) {
return ($state && $state instanceof State) ? $state->getStatekey() : 'noVal';
},
// This combination of 'expanded' and 'multiple' implements a select box
'expanded' => false,
'multiple' => false,
))
->add('transitions', CollectionType::class, array(
'entry_type' => TransitionType::class,
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'by_reference' => false,
'label' => 'transitionsTrans',
'entry_options' => array(
'states' => $this->workflow->getStates(),
),
))
->add('save', SubmitType::class, array(
'label' => 'submitTrans',
));
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => Workflow::class,
'translation_domain' => 'My_Bundle',
));
$resolver->setRequired(array(
'session'
));
}
}
提交$secondFormPart时,如何保存$firstFormPart中提交的$workflow的属性值?
【问题讨论】:
-
你能同时发布
PraWorkflowStatesType和PraWorkflowTransitionsType吗?我的猜测是PraWorkflowTransitionsType不包含PraWorkflowStatesType的字段,所以第二次提交后永远不会有第一个表单的数据。 -
@JoryGeerts 你是个天才!嘿 Mcfly,每个表单只处理我的实体
Workflow的几个属性还有什么意思?当然PraWorkflowTransitionsType不包含PraWorkflowStatesType的字段!它甚至是问题标题的一部分表单模型数据丢失了 字段不表示的属性值。 -
@JoryGeerts 现在你说在第二次提交之后它永远不会有第一个表单的数据。。当然可以,因为我传递了一个包含所有必要值的对象,但 Symfony 在渲染表单后不会保存这些值,顺便说一句,这是主要问题!
-
@JoryGeerts 我还是添加了表单类...
标签: php forms symfony symfony-forms