Merge pull request #2663 from ajshort/has-many-prepopulate

NEW: Pre-populate and disable foreign key field on has many lists.
This commit is contained in:
Ingo Schommer 2013-11-22 11:18:11 -08:00
commit 7e291133a0
2 changed files with 43 additions and 0 deletions

View File

@ -372,8 +372,26 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
$actions->push(new LiteralField('cancelbutton', $text));
}
}
$fields = $this->component->getFields();
if(!$fields) $fields = $this->record->getCMSFields();
// If we are creating a new record in a has-many list, then
// pre-populate the record's foreign key. Also disable the form field as
// it has no effect.
if($list instanceof HasManyList) {
$key = $list->getForeignKey();
$id = $list->getForeignID();
if(!$this->record->isInDB()) {
$this->record->$key = $id;
}
if($field = $fields->dataFieldByName($key)) {
$fields->makeFieldReadonly($field);
}
}
$form = new Form(
$this,
'ItemEditForm',

View File

@ -220,6 +220,31 @@ class GridFieldDetailFormTest extends FunctionalTest {
$form = $request->ItemEditForm();
$this->assertNotNull($form->Fields()->fieldByName('Callback'));
}
/**
* Tests that a has-many detail form is pre-populated with the parent ID.
*/
public function testHasManyFormPrePopulated() {
$group = $this->objFromFixture(
'GridFieldDetailFormTest_PeopleGroup', 'group'
);
$this->logInWithPermission('ADMIN');
$response = $this->get('GridFieldDetailFormTest_Controller');
$parser = new CSSContentParser($response->getBody());
$addLink = $parser->getBySelector('.ss-gridfield .new-link');
$addLink = (string) $addLink[0]['href'];
$response = $this->get($addLink);
$parser = new CSSContentParser($response->getBody());
$title = $parser->getBySelector('#GroupID span');
$id = $parser->getBySelector('#GroupID input');
$this->assertEquals($group->Name, (string) $title[0]);
$this->assertEquals($group->ID, (string) $id[0]['value']);
}
}
class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {