Skip to content

Instantly share code, notes, and snippets.

@zanderwar
Last active June 14, 2017 04:43
Show Gist options
  • Save zanderwar/d14f9172c1d7af785a2ca2d76eb95fc5 to your computer and use it in GitHub Desktop.
Save zanderwar/d14f9172c1d7af785a2ca2d76eb95fc5 to your computer and use it in GitHub Desktop.
Populates a SilverStripe Form subclass with DataObject values
<?php
class MyForm extends Form
{
/**
* @param Customer $customer
* @return $this
*/
public function setObject(DataObject $object)
{
$fields = self::Fields();
/** @var FormField $field */
foreach ($fields as $field) {
$value = $field->getName();
if ($object->{$value}) {
$field->setValue($object->{$value});
}
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment