From b93e94c0c38b954e16cf775f7f1d1cd60cbfd703 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 25 Jun 2018 11:06:43 +1200 Subject: [PATCH] FIX FormField::Link now throws a LogicException if no form is set yet --- src/Forms/FormField.php | 11 +++++++++-- tests/php/Forms/FormFieldTest.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index 8b6c3a571..502163b0b 100644 --- a/src/Forms/FormField.php +++ b/src/Forms/FormField.php @@ -2,6 +2,7 @@ namespace SilverStripe\Forms; +use LogicException; use ReflectionClass; use SilverStripe\Control\Controller; use SilverStripe\Control\RequestHandler; @@ -355,14 +356,20 @@ class FormField extends RequestHandler } /** - * Return a link to this field. + * Return a link to this field * * @param string $action - * * @return string + * @throws LogicException If no form is set yet */ public function Link($action = null) { + if (!$this->form) { + throw new LogicException( + 'Field must be associated with a form to call Link(). Please use $field->setForm($form);' + ); + } + $link = Controller::join_links($this->form->FormAction(), 'field/' . $this->name, $action); $this->extend('updateLink', $link, $action); return $link; diff --git a/tests/php/Forms/FormFieldTest.php b/tests/php/Forms/FormFieldTest.php index 07c5d618e..b63cbec76 100644 --- a/tests/php/Forms/FormFieldTest.php +++ b/tests/php/Forms/FormFieldTest.php @@ -384,4 +384,22 @@ class FormFieldTest extends SapphireTest $this->assertFalse($field->hasClass('banana')); $this->assertTrue($field->hasClass('cool-BAnana')); } + + public function testLinkWithForm() + { + $field = new FormField('Test'); + $form = new Form(null, 'Test', new FieldList, new FieldList); + $form->setFormAction('foo'); + $field->setForm($form); + $this->assertSame('foo/field/Test/bar', $field->Link('bar')); + } + + /** + * @expectedException \LogicException + */ + public function testLinkWithoutForm() + { + $field = new FormField('Test'); + $field->Link('bar'); + } }