我有一個控制器操作方法,它應該處理一個兩分裂的形式。每個表格只處理我的實體Workflow
的一些屬性。提交第一個表單後,我可以創建並呈現沒有問題的第二個表單。現在的問題:Symfony 3 - 表單模型數據丟失屬性值,而不是字段
提交第二表單後,所有的值在第一種形式中設置的信息都不見了,這意味着當調用submit
(或handleRequest
不會在這裏做任何區別)實體對象僅持有的數據屬性設置爲第一種形式,甚至無法正確解析某些值。
這裏是控制器(帶有一些註釋):
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':類,提交表單時保存通過工作流在當前會話和檢索它時,
buildForm
被稱爲2.時間?我的猜測是'PraWorkflowTransitionsType'不包含'PraWorkflowStatesType'的字段,所以在第二次提交後它永遠不會擁有第一個表單的數據。 –@JoryGeerts你是個天才!嘿Mcfly,還有什麼可以*每個表單只處理我的Entity'Workflow' *的一些屬性?意思是?當然'PraWorkflowTransitionsType'不包含'PraWorkflowStatesType'的字段!它甚至是問題標題的一部分*表格模型數據丟失屬性值,*** *不由字段***表示。 – goulashsoup
@JoryGeerts現在你在第二次提交後說*它永遠不會有第一個表單的數據。*。當然,這可能是因爲我傳遞了一個包含所有必需值的對象,但Symfony在渲染表單時不會保存這些值,這是主要問題! – goulashsoup