Skip to content

Instantly share code, notes, and snippets.

@sdaoudi
Forked from webmozart/MoneyType.php
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdaoudi/056560705c9ee0c098ee to your computer and use it in GitHub Desktop.
Save sdaoudi/056560705c9ee0c098ee to your computer and use it in GitHub Desktop.
class MoneyType extends AbstractType implements DataMapperInterface
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('amount', 'integer')
->add('currency', 'string')
->setDataMapper($this)
;
}
public function mapDataToForms($data, $forms)
{
foreach ($forms as $form) {
switch ($form->getName()) {
case 'amount':
$form->setData($data->getAmount());
break;
case 'currency':
$form->setData($data->getCurrency());
break;
}
}
}
public function mapFormsToData($forms, &$data)
{
$forms = iterator_to_array($forms);
$data = new Money($forms['amount']->getData(), $forms['currency']->getData());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment