Skip to content

Instantly share code, notes, and snippets.

@NicolasBadey
Created October 9, 2012 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NicolasBadey/3858213 to your computer and use it in GitHub Desktop.
Save NicolasBadey/3858213 to your computer and use it in GitHub Desktop.
FormFlowHandler
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;
}
}
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() {
}
}
class MyFlowFormHandler extends BaseFormFlowHandler
[...]
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