FIX: Ensure GridFieldDetailForm has the current record validator.

Currently the validator is only set through
ModelAdmin and not through GridField
directly. This will set the validator based on the
record unless a custom validator has been
provided.

http://www.silverstripe.org/data-model-questions/show/34650?start=8#post331958
This commit is contained in:
Will Rossiter 2014-01-11 15:40:48 +13:00
parent 5e3420a640
commit 4ea62b44f9
2 changed files with 95 additions and 2 deletions

View File

@ -90,6 +90,12 @@ class GridFieldDetailForm implements GridField_URLHandler {
$handler = Object::create($class, $gridField, $this, $record, $controller, $this->name); $handler = Object::create($class, $gridField, $this, $record, $controller, $this->name);
$handler->setTemplate($this->template); $handler->setTemplate($this->template);
// if no validator has been set on the GridField and the record has a
// CMS validator, use that.
if(!$this->getValidator() && method_exists($record, 'getCMSValidator')) {
$this->setValidator($record->getCMSValidator());
}
return $handler->handleRequest($request, DataModel::inst()); return $handler->handleRequest($request, DataModel::inst());
} }

View File

@ -1,6 +1,11 @@
<?php <?php
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest extends FunctionalTest { class GridFieldDetailFormTest extends FunctionalTest {
protected static $fixture_file = 'GridFieldDetailFormTest.yml'; protected static $fixture_file = 'GridFieldDetailFormTest.yml';
protected $extraDataObjects = array( protected $extraDataObjects = array(
@ -9,6 +14,48 @@ class GridFieldDetailFormTest extends FunctionalTest {
'GridFieldDetailFormTest_Category', 'GridFieldDetailFormTest_Category',
); );
public function testValidator() {
$this->logInWithPermission('ADMIN');
$response = $this->get('GridFieldDetailFormTest_Controller');
$this->assertFalse($response->isError());
$parser = new CSSContentParser($response->getBody());
$addlinkitem = $parser->getBySelector('.ss-gridfield .new-link');
$addlink = (string) $addlinkitem[0]['href'];
$response = $this->get($addlink);
$this->assertFalse($response->isError());
$parser = new CSSContentParser($response->getBody());
$addform = $parser->getBySelector('#Form_ItemEditForm');
$addformurl = (string) $addform[0]['action'];
$response = $this->post(
$addformurl,
array(
'FirstName' => 'Jeremiah',
'ajax' => 1,
'action_doSave' => 1
)
);
$parser = new CSSContentParser($response->getBody());
$errors = $parser->getBySelector('span.required');
$this->assertEquals(1, count($errors));
$response = $this->post(
$addformurl,
array(
'ajax' => 1,
'action_doSave' => 1
)
);
$parser = new CSSContentParser($response->getBody());
$errors = $parser->getBySelector('span.required');
$this->assertEquals(2, count($errors));
}
public function testAddForm() { public function testAddForm() {
$this->logInWithPermission('ADMIN'); $this->logInWithPermission('ADMIN');
$group = GridFieldDetailFormTest_PeopleGroup::get() $group = GridFieldDetailFormTest_PeopleGroup::get()
@ -247,7 +294,13 @@ class GridFieldDetailFormTest extends FunctionalTest {
} }
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_Person extends DataObject implements TestOnly { class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {
private static $db = array( private static $db = array(
'FirstName' => 'Varchar', 'FirstName' => 'Varchar',
'Surname' => 'Varchar' 'Surname' => 'Varchar'
@ -280,7 +333,18 @@ class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {
); );
return $fields; return $fields;
} }
public function getCMSValidator() {
return new RequiredFields(array(
'FirstName', 'Surname'
));
} }
}
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly { class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly {
private static $db = array( private static $db = array(
@ -306,6 +370,11 @@ class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly
} }
} }
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_Category extends DataObject implements TestOnly { class GridFieldDetailFormTest_Category extends DataObject implements TestOnly {
private static $db = array( private static $db = array(
@ -331,6 +400,11 @@ class GridFieldDetailFormTest_Category extends DataObject implements TestOnly {
} }
} }
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_Controller extends Controller implements TestOnly { class GridFieldDetailFormTest_Controller extends Controller implements TestOnly {
private static $allowed_actions = array('Form'); private static $allowed_actions = array('Form');
@ -354,6 +428,11 @@ class GridFieldDetailFormTest_Controller extends Controller implements TestOnly
} }
} }
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_GroupController extends Controller implements TestOnly { class GridFieldDetailFormTest_GroupController extends Controller implements TestOnly {
private static $allowed_actions = array('Form'); private static $allowed_actions = array('Form');
@ -370,6 +449,11 @@ class GridFieldDetailFormTest_GroupController extends Controller implements Test
} }
} }
/**
* @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_CategoryController extends Controller implements TestOnly { class GridFieldDetailFormTest_CategoryController extends Controller implements TestOnly {
private static $allowed_actions = array('Form'); private static $allowed_actions = array('Form');
@ -391,5 +475,8 @@ class GridFieldDetailFormTest_CategoryController extends Controller implements T
} }
} }
class GridFieldDetailFormTest_ItemRequest extends GridFieldDetailForm_ItemRequest implements TestOnly { /**
} * @package framework
* @subpackage tests
*/
class GridFieldDetailFormTest_ItemRequest extends GridFieldDetailForm_ItemRequest implements TestOnly { }