Merge pull request #8213 from creative-commoners/pulls/4.2/falsy-forms

FIX FormField::Link works when no form is currently set
This commit is contained in:
Loz Calver 2018-07-16 13:53:58 +01:00 committed by GitHub
commit b317bf163c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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');
}
}