-
-
Save mickaelandrieu/3858836 to your computer and use it in GitHub Desktop.
FormFlowHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ...; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Form\Form; | |
abstract class BaseFormFlowHandler extends BaseFormHandler | |
{ | |
protected $step; | |
protected $request; | |
public function __construct($formType, $formFactory,$request) | |
{ | |
$this->formType = $formType; | |
$this->formFactory = $formFactory; | |
$this->request = $request; | |
} | |
public function createForm($step, $formData = null, $groups = null) | |
{ | |
$this->form = $this->formFactory->create($this->formType, $formData, [ | |
'step' => $step, | |
'validation_groups' => $groups | |
]); | |
$this->setCurrentStep($step); | |
} | |
public function setCurrentStep($step) | |
{ | |
$this->step = $step; | |
} | |
public function getCurrentStep() | |
{ | |
return $this->step; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Form\Form; | |
abstract class BaseFormHandler { | |
protected $request; | |
protected $form; | |
public function __construct(Form $form, Request $request) { | |
$this->request = $request; | |
$this->form = $form; | |
} | |
public function getForm() { | |
return $this->form; | |
} | |
public function process($entity = null) { | |
if ($entity) | |
$this->form->setData($entity); | |
if ('POST' == $this->request->getMethod()) { | |
$this->form->bindRequest($this->request); | |
if ($this->form->isValid()) { | |
$this->onSuccess(); | |
return true; | |
} | |
} | |
return false; | |
} | |
public function onSuccess() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFlowFormHandler extends BaseFormFlowHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
switch ($options['step']) { | |
case 1: | |
[...] | |
} | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
[...] | |
'step' => 1 | |
)); | |
} | |
[...] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment