Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FRAM-97] Rebuild inner form on CustomForm::setContainer #6

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/Custom/CustomForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Bdf\Form\RootElementInterface;
use Bdf\Form\View\ElementViewInterface;
use Iterator;
use WeakReference;

/**
* Utility class for simply create a custom form element
Expand Down Expand Up @@ -60,6 +61,11 @@ abstract class CustomForm implements FormInterface
*/
private $form;

/**
* @var WeakReference<ChildInterface>|null
*/
private $container;

/**
* @var list<callable(static, FormBuilderInterface): void>
*/
Expand Down Expand Up @@ -187,7 +193,7 @@ public function error(?HttpFieldPath $field = null): FormError
*/
public function container(): ?ChildInterface
{
return $this->form()->container();
return $this->container ? $this->container->get() : null;
}

/**
Expand All @@ -196,7 +202,8 @@ public function container(): ?ChildInterface
public function setContainer(ChildInterface $container): ElementInterface
{
$form = clone $this;
$form->form = $this->form()->setContainer($container);
$form->container = WeakReference::create($container);
$form->form = null; // Reset the form to ensure that $this references will be regenerated

return $form;
}
Expand Down Expand Up @@ -304,16 +311,26 @@ final protected function form(): FormInterface
return $this->form;
}

// Form can be rebuilt, so we need to clone the builder to avoid side effects
$builder = clone $this->builder;

/** @var static $this Psalm cannot infer this type */

foreach ($this->preConfigureHooks as $hook) {
$hook($this, $this->builder);
$hook($this, $builder);
}

/** @psalm-suppress ArgumentTypeCoercion */
$this->configure($this->builder);
$this->configure($builder);

$form = $builder->buildElement();

if ($this->container && $container = $this->container->get()) {
$form = $form->setContainer($container);
}

$this->form = $form;

$form = $this->form = $this->builder->buildElement();
$this->postConfigure($form);

foreach ($this->postConfigureHooks as $hook) {
Expand Down
65 changes: 65 additions & 0 deletions tests/Custom/CustomFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
namespace Bdf\Form\Custom;

use Bdf\Form\Aggregate\ArrayElement;
use Bdf\Form\Aggregate\Collection\ChildrenCollection;
use Bdf\Form\Aggregate\FooForm;
use Bdf\Form\Aggregate\Form;
use Bdf\Form\Aggregate\FormBuilder;
use Bdf\Form\Aggregate\FormBuilderInterface;
use Bdf\Form\Aggregate\FormInterface;
use Bdf\Form\Aggregate\RootForm;
use Bdf\Form\Aggregate\View\FormView;
use Bdf\Form\Child\Child;
use Bdf\Form\Child\ChildInterface;
use Bdf\Form\Child\Http\HttpFieldPath;
use Bdf\Form\ElementInterface;
use Bdf\Form\Error\FormError;
use Bdf\Form\Error\FormErrorPrinterInterface;
use Bdf\Form\Leaf\IntegerElement;
use Bdf\Form\Leaf\StringElement;
use Bdf\Form\Util\FieldFinderTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\LessThan;

Expand Down Expand Up @@ -461,6 +466,47 @@ public function postConfigure(FormInterface $form): void
$this->assertInstanceOf(Form::class, $form->param);
$this->assertInstanceOf(StringElement::class, $form->param['foo']->element());
}

public function test_sibling_into_an_embedded_custom_form()
{
$form = new class extends CustomForm {
protected function configure(FormBuilderInterface $builder): void
{
$builder->add('foo', EmbeddedWithFieldFinderForm::class);
}
};

$form->submit(['foo' => ['bar' => 'abc', 'baz' => ['a', 'b', 'c']]]);

$this->assertTrue($form->valid());

$form->submit(['foo' => ['bar' => 'abcd', 'baz' => ['a', 'b', 'c']]]);
$this->assertFalse($form->valid());
}

public function test_setContainer_should_rebuild_form()
{
$form = new class extends CustomForm {
public $value = 'foo';

protected function configure(FormBuilderInterface $builder): void
{
$builder->string('foo')->setter(function () { return $this->value; });
}
};

$form->value = 'bar';
$this->assertEquals('bar', $form->submit([])->value()['foo']);

$parent = new Form(new ChildrenCollection());
$child = new Child('a', $form);
$child->setParent($parent);

$newForm = $form->setContainer($child);
$newForm->value = 'baz';
$this->assertEquals('bar', $form->submit([])->value()['foo']);
$this->assertEquals('baz', $newForm->submit([])->value()['foo']);
}
}

/**
Expand Down Expand Up @@ -508,3 +554,22 @@ class Person

public $notDeclaredOnForm;
}

class EmbeddedWithFieldFinderForm extends CustomForm
{
use FieldFinderTrait;

protected function configure(FormBuilderInterface $builder): void
{
$builder
->array('baz')
->depends('bar')
->arrayConstraint(function ($value) {
$bar = $this->findFieldValue('bar');

return count($value) === strlen($bar);
})
;
$builder->string('bar');
}
}