diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b48fc2..75ba6ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG ========= +0.2.3 +----- + * Add `buildViewFlow()` and `finishViewFlow` methods to `FormFlowTypeInterface` + 0.2.2 ----- * Add `AbstractFlowButtonType` to simplify button type creation diff --git a/src/Form/Flow/AbstractFlowType.php b/src/Form/Flow/AbstractFlowType.php index e9c655e..0a801ff 100644 --- a/src/Form/Flow/AbstractFlowType.php +++ b/src/Form/Flow/AbstractFlowType.php @@ -4,6 +4,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Yceruto\FormFlowBundle\Form\Flow\Type\FormFlowType; abstract class AbstractFlowType extends AbstractType implements FormFlowTypeInterface @@ -15,10 +17,32 @@ final public function buildForm(FormBuilderInterface $builder, array $options): $this->buildFormFlow($builder, $options); } + final public function buildView(FormView $view, FormInterface $form, array $options): void + { + \assert($form instanceof FormFlowInterface); + + $this->buildViewFlow($view, $form, $options); + } + + final public function finishView(FormView $view, FormInterface $form, array $options): void + { + \assert($form instanceof FormFlowInterface); + + $this->finishViewFlow($view, $form, $options); + } + public function buildFormFlow(FormFlowBuilderInterface $builder, array $options): void { } + public function buildViewFlow(FormView $view, FormFlowInterface $form, array $options): void + { + } + + public function finishViewFlow(FormView $view, FormFlowInterface $form, array $options): void + { + } + public function getParent(): string { return FormFlowType::class; diff --git a/src/Form/Flow/FormFlowTypeInterface.php b/src/Form/Flow/FormFlowTypeInterface.php index 5a3febd..442242b 100644 --- a/src/Form/Flow/FormFlowTypeInterface.php +++ b/src/Form/Flow/FormFlowTypeInterface.php @@ -3,11 +3,50 @@ namespace Yceruto\FormFlowBundle\Form\Flow; use Symfony\Component\Form\FormTypeInterface; +use Symfony\Component\Form\FormView; /** * A type that should be converted into a {@link FormFlow} instance. */ interface FormFlowTypeInterface extends FormTypeInterface { + /** + * Builds the multistep form. + * + * This method is called for each multistep type. Type extensions can further + * modify the multistep form. + * + * @param array $options + */ public function buildFormFlow(FormFlowBuilderInterface $builder, array $options): void; + + /** + * Builds the multistep form view. + * + * This method is called for each multistep type. Type extensions can further + * modify the view. + * + * A view of a multistep form is built before the views of the child forms are built. + * This means that you cannot access child views in this method. If you need + * to do so, move your logic to {@link finishViewFlow()} instead. + * + * @param array $options + */ + public function buildViewFlow(FormView $view, FormFlowInterface $form, array $options): void; + + /** + * Finishes the multistep form view. + * + * This method gets called for each multistep type. Type extensions can further + * modify the view. + * + * When this method is called, views of the multistep form's children have already + * been built and finished and can be accessed. You should only implement + * such logic in this method that actually accesses child views. For everything + * else you are recommended to implement {@link buildViewFlow()} instead. + * + * @param array $options + */ + public function finishViewFlow(FormView $view, FormFlowInterface $form, array $options): void; + } diff --git a/src/Form/Flow/Type/FormFlowType.php b/src/Form/Flow/Type/FormFlowType.php index f60dd70..4483030 100644 --- a/src/Form/Flow/Type/FormFlowType.php +++ b/src/Form/Flow/Type/FormFlowType.php @@ -5,7 +5,6 @@ use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; -use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\OptionsResolver\Options; @@ -42,10 +41,8 @@ public function buildFormFlow(FormFlowBuilderInterface $builder, array $options) $builder->addEventListener(FormEvents::PRE_SUBMIT, $this->onPreSubmit(...), -100); } - public function buildView(FormView $view, FormInterface $form, array $options): void + public function buildViewFlow(FormView $view, FormFlowInterface $form, array $options): void { - \assert($form instanceof FormFlowInterface); - $view->vars['cursor'] = $cursor = $form->getCursor(); $index = 0;