/** * 30/06/2009 - Enhancement: * SaveInto checks if set-methods are available and use them * instead of setting the values in the money class directly. saveInto * initiates a new Money class object to pass through the values to the setter * method. * * (see @link MoneyFieldTest_CustomSetter_Object for more information) * * @param DataObjectInterface|Object $dataObject */ public function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->getName(); if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create_field('Money', array("Currency" => $this->fieldCurrency->dataValue(), "Amount" => $this->fieldAmount->dataValue())); } else { $currencyField = "{$fieldName}Currency"; $amountField = "{$fieldName}Amount"; $dataObject->{$currencyField} = $this->fieldCurrency->dataValue(); $dataObject->{$amountField} = $this->fieldAmount->dataValue(); } }
/** * Test that data loaded in via Form::loadDataFrom(DataObject) will populate the field correctly, * and can format the database value appropriately for the frontend * * @param string $locale * @param array $tests */ public function checkDataFormatting($locale, $tests) { i18n::set_locale($locale); $field = new NumericField('Number'); /** @skipUpgrade */ $form = new Form(new Controller(), 'Form', new FieldList($field), new FieldList()); $dataObject = new NumericFieldTest_Object(); foreach ($tests as $input => $output) { // Given a dataobject as a context, the field should assume the field value is not localised $dataObject->Number = (string) $input; $form->loadDataFrom($dataObject, Form::MERGE_CLEAR_MISSING); // Test value $this->assertEquals($input, $field->dataValue(), "Expected {$input} loaded via dataobject to be left intact in locale {$locale}"); // Test expected formatted value (Substitute nbsp for spaces) $this->assertEquals($this->clean($output), $field->Value(), "Expected {$input} to be formatted as {$output} in locale {$locale}"); } }