이들은 SF2에 대한 첫 번째 단계입니다. 다른 엔티티가 포함 된 엔티티에 대해 다중 단계 양식을 설정하려고합니다.Symfony2가 하나의 결과로 다중 단계 형식을 병합합니다.
내가 (단축) 양식 유형
class ApplicationFormType extends AbstractType
{
protected $_step = 1;
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($this->_step == 1) {
$builder
->add('patient', new Type\PersonType())
->add('insurance', 'entity', array(
'class' => 'Acme\MainBundle\Entity\Insurance',
));
} elseif ($this->_step == 2) {
$nurse = new Type\PersonType();
$nurse->requireBareData();
$builder
->add('nurse', $nurse)
->add('nurse_type', 'entity', array(
'class' => 'Acme\MainBundle\Entity\NurseType',
'expanded' => true,
'multiple' => false,
))
->add('nursing_support', 'checkbox', array(
'required' => false,
));
}
}
public function setStep($step)
{
$this->_step = $step;
}
내 컨트롤러의 모양을했습니다
public function assistAction() {
$request = $this->getRequest();
$step = $this->getSession()->get('assist_step', 1);
if ($request->getMethod() != 'POST') {
$step = 1;
$this->getSession()->set('assist_data', NULL);
}
$this->getSession()->set('assist_step', $step);
$application = new Application();
$type = new ApplicationFormType();
$type->setStep($step);
$form = $this->createForm($type, $application, array(
'validation_groups' => array($step == 1 ? 'FormStepOne' : 'FormStepTwo'),
));
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
if ($step == 1) {
$data = $form->getData();
$this->getSession()->set('assist_data', serialize($data));
$this->getSession()->set('assist_step', ++$step);
$type = new ApplicationFormType();
$type->setStep($step);
$form = $this->createForm($type, $application, array(
'validation_groups' => array('FormStepTwo'),
));
} else {
$step_1 = unserialize($this->getSession()->get('assist_data', ''));
$data = $form->getData();
=> $data->setPatient($step_1->getPatient());
=> $data->setInsurance($step_1->getInsurance());
$this->getSession()->set('assist_data', NULL);
$this->getSession()->set('assist_step', 1);
}
}
}
return $this->render('Acme:Default:onlineassist.html.twig', array(
'form' => $form->createView(),
'step' => $step,
));
}
같은 내 질문입니다, 내가 별도로 첫 번째 양식 단계를 "복사"속성이 있다면, 같은
$data->setPatient($step_1->getPatient());
$data->setInsurance($step_1->getInsurance());
또는 I 번째 형태 인트으로부터 데이터 세션의 데이터를 직렬화를 병합 피? 아니면 완전히 다른 접근 방식이 있습니까?
다중 단계 양식의 궁극적 인 목적은 무엇입니까? 프로세스 전반에 걸쳐 제출 된 데이터 (유효하다면)를 유지하고 클라이언트가 데이터를 검토 한 다음 DB에 삽입하면 정확합니까? –
@ThomasPotaire : 정확하지 않습니다. 클라이언트는 데이터를 검토하지 않습니다. FormType에서 볼 수 있듯이 두 번째 단계는 사용자가 채울 다른 엔터티 필드를 제공합니다. (그리고 세 번째 단계가 있습니다.) 사용자가 모든 단계를 완료하면 (btw : 두 번째 옵션은 곧 선택 될 것입니다.) 모든 관련 엔티티가있는 'Application'엔티티를 DB에 유지해야합니다. – rabudde