Skip to content

Added a clientToApp method for use in the BindClientData event #3764

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,31 @@ private function clientToNorm($value)
return $value;
}

/**
* Helper function for use in BIND_CLIENT_DATA event.
*
* @params string $name The name of the child field
* @params array $clientData the client data from the event
*
* @return mixed
*/
public function getChildNormData($name, $clientData)
{
if (!$this->hasChildren()) {
throw new \InvalidArgumentException('This form has no children');
}

$form = $this->get($name);

$event = new DataEvent($form, $clientData[$name]);
$this->dispatcher->dispatch(FormEvents::PRE_BIND, $event);

$event = new FilterDataEvent($form, $clientData[$name]);
$this->dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event);

return $form->clientToNorm($event->getData());
}

/**
* Validates whether the given variable is a valid form name.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Form/Tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,21 @@ public function testBindMapsBoundChildrenOntoExistingClientData()
));
}

public function testClientToApp()
{
$form = $this->getBuilder()->getForm();

$form->add($this->getBuilder('test')
->appendClientTransformer(new FixedDataTransformer(array(
'' => '',
'b' => 'a'
)))
->getForm()
);

$this->assertEquals('b', $form->getChildNormData('test', array('test' => 'a')));
}

public function testBindMapsBoundChildrenOntoEmptyData()
{
$test = $this;
Expand Down