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

Support data binding for CollectionType #13

Closed
u0816 opened this issue Jan 4, 2024 · 1 comment · Fixed by #14
Closed

Support data binding for CollectionType #13

u0816 opened this issue Jan 4, 2024 · 1 comment · Fixed by #14
Assignees
Labels

Comments

@u0816
Copy link

u0816 commented Jan 4, 2024

Running this code doesn't prefill the group fields:

class PersonGroupType extends AbstractGroupType
{
    public function build(): void
    {
        $this->add('first_name', TextType::class);
        $this->add('last_name', TextType::class);
    }
}

$data = [
    'people' => [
        ['first_name' => 'Peter', 'last_name' => 'Parker']
    ]
];

$builder = new FormBuilder([
    'key' => 'collection_example',
    'method' => 'POST',
    'html_validation' => false,
], $data);

$builder->add('people', CollectionType::class, ['entry_type' => PersonGroupType::class]);    
$builder->add('send_message', 'submit');
$form = $builder->getForm();

The method CollectionType::buildEntry could pass the data to the children:

private function buildEntry(int $position = 0, $data = null): TypeInterface
{
    $entryType = $this->entryType;
    /** @var TypeInterface $entry */
    $entry = new $entryType($this->entryOptions);
    $entry
        ->setParent($this)
        ->setName($this->name)
        ->setPosition($position)
    ;

    if ($entry->getLabel() === null) {
        $entry->setLabel($this->getHumanName());
    }

    $entry->build();

    if (\func_num_args() > 0) {
        $entry->setData($data);

        //NEW
        if(is_array($data)) {
            foreach ($entry->all() as $child) {
                if(isset($data[$child->getName()])) $child->setData($data[$child->getName()]);
            }
        }
        
    }

    return $entry;
}  
@andyexeter andyexeter self-assigned this Jan 5, 2024
@andyexeter andyexeter added the bug label Jan 5, 2024
@andyexeter andyexeter changed the title Support data binding for GroupType Support data binding for CollectionType Jan 5, 2024
@andyexeter andyexeter linked a pull request Jan 5, 2024 that will close this issue
@andyexeter
Copy link
Member

Hey there,

Thanks for reporting this and providing a fix!

I've created a PR for this: #14 - could you test it and see if it resolves the issue for you?

If you want to install the PR branch via composer you can run the following command:

composer require palmtree/form:dev-13-support-data-binding-for-collectiontype

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants