diff --git a/forms/gridfield/GridFieldDetailForm.php b/forms/gridfield/GridFieldDetailForm.php index 108af996d..188565c39 100644 --- a/forms/gridfield/GridFieldDetailForm.php +++ b/forms/gridfield/GridFieldDetailForm.php @@ -90,6 +90,12 @@ class GridFieldDetailForm implements GridField_URLHandler { $handler = Object::create($class, $gridField, $this, $record, $controller, $this->name); $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()); } diff --git a/tests/forms/gridfield/GridFieldDetailFormTest.php b/tests/forms/gridfield/GridFieldDetailFormTest.php index 881b13941..5e0c30f22 100644 --- a/tests/forms/gridfield/GridFieldDetailFormTest.php +++ b/tests/forms/gridfield/GridFieldDetailFormTest.php @@ -1,6 +1,11 @@ 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() { $this->logInWithPermission('ADMIN'); $group = GridFieldDetailFormTest_PeopleGroup::get() @@ -247,7 +294,13 @@ class GridFieldDetailFormTest extends FunctionalTest { } +/** + * @package framework + * @subpackage tests + */ + class GridFieldDetailFormTest_Person extends DataObject implements TestOnly { + private static $db = array( 'FirstName' => 'Varchar', 'Surname' => 'Varchar' @@ -280,8 +333,19 @@ class GridFieldDetailFormTest_Person extends DataObject implements TestOnly { ); return $fields; } + + public function getCMSValidator() { + return new RequiredFields(array( + 'FirstName', 'Surname' + )); + } } +/** + * @package framework + * @subpackage tests + */ + class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly { private static $db = array( 'Name' => 'Varchar' @@ -306,6 +370,11 @@ class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly } } +/** + * @package framework + * @subpackage tests + */ + class GridFieldDetailFormTest_Category extends DataObject implements TestOnly { 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 { 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 { 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 { 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 { -} \ No newline at end of file +/** + * @package framework + * @subpackage tests + */ +class GridFieldDetailFormTest_ItemRequest extends GridFieldDetailForm_ItemRequest implements TestOnly { } \ No newline at end of file