mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #11150 from beerbohmdo/allow_override_moneyfield
Allow better subclassing of MoneyField
This commit is contained in:
commit
c2b606c24c
@ -59,11 +59,7 @@ class MoneyField extends FormField
|
|||||||
public function __construct($name, $title = null, $value = "")
|
public function __construct($name, $title = null, $value = "")
|
||||||
{
|
{
|
||||||
$this->setName($name);
|
$this->setName($name);
|
||||||
$this->fieldAmount = NumericField::create(
|
$this->buildAmountField();
|
||||||
"{$name}[Amount]",
|
|
||||||
_t('SilverStripe\\Forms\\MoneyField.FIELDLABELAMOUNT', 'Amount')
|
|
||||||
)
|
|
||||||
->setScale(2);
|
|
||||||
$this->buildCurrencyField();
|
$this->buildCurrencyField();
|
||||||
|
|
||||||
parent::__construct($name, $title, $value);
|
parent::__construct($name, $title, $value);
|
||||||
@ -75,6 +71,18 @@ class MoneyField extends FormField
|
|||||||
$this->fieldCurrency = clone $this->fieldCurrency;
|
$this->fieldCurrency = clone $this->fieldCurrency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a field to input the amount of money
|
||||||
|
*/
|
||||||
|
protected function buildAmountField(): void
|
||||||
|
{
|
||||||
|
$this->fieldAmount = NumericField::create(
|
||||||
|
$this->name . '[Amount]',
|
||||||
|
_t('SilverStripe\\Forms\\MoneyField.FIELDLABELAMOUNT', 'Amount')
|
||||||
|
)
|
||||||
|
->setScale(2);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a new currency field based on the allowed currencies configured
|
* Builds a new currency field based on the allowed currencies configured
|
||||||
*
|
*
|
||||||
|
@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\Tests;
|
namespace SilverStripe\Forms\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Forms\DropdownField;
|
||||||
|
use SilverStripe\Forms\HiddenField;
|
||||||
|
use SilverStripe\Forms\NumericField;
|
||||||
use SilverStripe\Forms\RequiredFields;
|
use SilverStripe\Forms\RequiredFields;
|
||||||
use SilverStripe\Forms\Tests\MoneyFieldTest\CustomSetter_Object;
|
use SilverStripe\Forms\Tests\MoneyFieldTest\CustomSetter_Object;
|
||||||
use SilverStripe\Forms\Tests\MoneyFieldTest\TestObject;
|
use SilverStripe\Forms\Tests\MoneyFieldTest\TestObject;
|
||||||
|
use SilverStripe\Forms\TextField;
|
||||||
use SilverStripe\ORM\FieldType\DBMoney;
|
use SilverStripe\ORM\FieldType\DBMoney;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\Forms\MoneyField;
|
use SilverStripe\Forms\MoneyField;
|
||||||
@ -127,4 +131,31 @@ class MoneyFieldTest extends SapphireTest
|
|||||||
]);
|
]);
|
||||||
$this->assertFalse($field->validate($validator));
|
$this->assertFalse($field->validate($validator));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetCurrencyField(): void
|
||||||
|
{
|
||||||
|
$field = new MoneyField('Money');
|
||||||
|
$field->setAllowedCurrencies(['NZD', 'USD']);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(DropdownField::class, $field->getCurrencyField());
|
||||||
|
$this->assertEquals('Money[Currency]', $field->getCurrencyField()->getName());
|
||||||
|
|
||||||
|
$field->setAllowedCurrencies(['USD']);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(HiddenField::class, $field->getCurrencyField());
|
||||||
|
$this->assertEquals('Money[Currency]', $field->getCurrencyField()->getName());
|
||||||
|
|
||||||
|
$field->setAllowedCurrencies([]);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(TextField::class, $field->getCurrencyField());
|
||||||
|
$this->assertEquals('Money[Currency]', $field->getCurrencyField()->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAmountField(): void
|
||||||
|
{
|
||||||
|
$field = new MoneyField('Money');
|
||||||
|
$this->assertInstanceOf(NumericField::class, $field->getAmountField());
|
||||||
|
$this->assertEquals(2, $field->getAmountField()->getScale());
|
||||||
|
$this->assertEquals('Money[Amount]', $field->getAmountField()->getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user