diff --git a/src/Dev/FormPageController.php b/src/Dev/FormPageController.php index deabbb6..bfb30ef 100644 --- a/src/Dev/FormPageController.php +++ b/src/Dev/FormPageController.php @@ -17,6 +17,7 @@ use Syntro\SilverstripeBootstrapForms\Forms\TextareaField; use Syntro\SilverstripeBootstrapForms\Forms\TextField; use Syntro\SilverstripeBootstrapForms\Forms\FieldGroup; +use Syntro\SilverstripeBootstrapForms\Forms\ReadonlyField; /** * demo controller @@ -47,6 +48,7 @@ public function Form() $emailfield = EmailField::create('emailfield', 'emailfield'), $optionsetfield = OptionsetField::create('optionsetfield', 'optionsetfield', ['a' => 'value a', 'b' => 'value b']), $phonefield = PhoneField::create('phonefield', 'phonefield'), + $readonlyfield = ReadonlyField::create('readonlyfield', 'readonlyfield'), $textareafield = TextareaField::create('textareafield', 'textareafield'), $textfield = TextField::create('textfield', 'textfield'), ); @@ -57,6 +59,7 @@ public function Form() $emailfield->addHolderClass('emailfieldholderclass')->addExtraClass('emailfieldextraclass'); $optionsetfield->addHolderClass('optionsetfieldholderclass')->addExtraClass('optionsetfieldextraclass'); $phonefield->addHolderClass('phonefieldholderclass')->addExtraClass('phonefieldextraclass'); + $readonlyfield->addHolderClass('readonlyfieldholderclass')->addExtraClass('readonlyfieldextraclass'); $textareafield->addHolderClass('textareafieldholderclass')->addExtraClass('textareafieldextraclass'); $textfield->addHolderClass('textfieldholderclass')->addExtraClass('textfieldextraclass'); diff --git a/src/Forms/ReadonlyField.php b/src/Forms/ReadonlyField.php new file mode 100644 index 0000000..d9512f2 --- /dev/null +++ b/src/Forms/ReadonlyField.php @@ -0,0 +1,32 @@ + + */ +class ReadonlyField extends BackendReadonlyField +{ + use HolderClass; + + /** + * Returns a readonly field. + * + * @param string $name name of the field + * @param null|string $title title of the field + * @param null|string $value value of the field + * + * + */ + function __construct($name, $title = null, $value = null) + { + $this->setFieldHolderTemplate('Syntro\\SilverstripeBootstrapForms\\Forms\\FormField_holder'); + $this->setSmallFieldHolderTemplate('Syntro\\SilverstripeBootstrapForms\\Forms\\FormField_holder_small'); + $this->setupDefaultHolderClasses(); + parent::__construct($name, $title, $value); + } +} diff --git a/tests/RenderFormFieldsTest.php b/tests/RenderFormFieldsTest.php index c241fe4..f5a5c3b 100644 --- a/tests/RenderFormFieldsTest.php +++ b/tests/RenderFormFieldsTest.php @@ -8,10 +8,11 @@ use Syntro\SilverstripeBootstrapForms\Forms\CheckboxSetField; use Syntro\SilverstripeBootstrapForms\Forms\DropdownField; use Syntro\SilverstripeBootstrapForms\Forms\EmailField; +use Syntro\SilverstripeBootstrapForms\Forms\FieldGroup; use Syntro\SilverstripeBootstrapForms\Forms\OptionsetField; +use Syntro\SilverstripeBootstrapForms\Forms\ReadonlyField; use Syntro\SilverstripeBootstrapForms\Forms\TextareaField; use Syntro\SilverstripeBootstrapForms\Forms\TextField; -use Syntro\SilverstripeBootstrapForms\Forms\FieldGroup; /** * Test the form fields generation @@ -143,6 +144,18 @@ public function testPhoneFieldRendering() $this->assertStringContainsString('', $body); } + public function testReadonlyFieldRendering() + { + $formPage = $this->objFromFixture(FormPage::class, 'page'); + $formPage->copyVersionToStage('Stage', 'Live'); + + $page = $this->get('/form'); + $body = $page->getBody(); + + $this->assertStringContainsString('
', $body); + $this->assertStringContainsString('', $body); + $this->assertStringContainsString('', $body); + } /** * testTextareaFieldRendering - description