FIX FormField::Link now throws a LogicException if no form is set yet

This commit is contained in:
Robbie Averill 2018-06-25 11:06:43 +12:00 committed by Daniel Hensby
parent 27b60ae989
commit b93e94c0c3
No known key found for this signature in database
GPG Key ID: D8DEBC4C8E7BC8B9
2 changed files with 27 additions and 2 deletions

View File

@ -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;

View File

@ -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');
}
}