mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX FormField::Link now throws a LogicException if no form is set yet
This commit is contained in:
parent
27b60ae989
commit
b93e94c0c3
@ -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;
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user