Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DataObject has_one DropdownField


Go to End


4 Posts   4548 Views

Avatar
gregsaunders

Community Member, 12 Posts

13 April 2011 at 9:26am

I think someone needs to point me back to the basics here. I'm a SilverStripe noobie and am having a terrible time trying to get a has_one from a DataObject into DropdownField on a form, saving that form and saving the selected option.

So I can create the form, I can populate the DropdownField with the correct options. I cannot, however, figure out how to set the default value of the select box (from the existing dataobject) and I can't figure out how to save the selected item.

I've tried setting the fourth argument to DropdownField but it seems to be ignored, or wiped out when I loadDataFrom.

Help!
Greg

class FamilyMember extends DataObject {
static $db = array(
'FirstName' => 'Varchar(255)',
'LastName' => 'Varchar(255)',
...
'Email' => 'Varchar(100)'
);

static $has_one = array(
'Family' => 'Family',
'MemberType' => 'MemberType'
);

public static $summary_fields = array(
'FirstName' => 'FirstName',
'LastName' => 'LastName'
);

static $default_sort = 'DateOfBirth';
}

(from controller)

function Form() {
$member_type_data = DataObject::get('MemberType');
$member_type_source = $member_type_data->toDropDownMap('ID','Name');

$fields = new FieldSet(
new HiddenField('ID', 'ID'),
new TextField('FirstName', 'First Name'),
new TextField('LastName', 'Last Name'),
...
new DropdownField('MemberType', 'Member Type', $member_type_source)
);
$actions = new FieldSet(
new FormAction('doSubmitMember', 'Submit')
);
$validator = new RequiredFields(
'FirstName', 'LastName', 'DateOfBirth', 'MemberType'
);
$form = new Form(
$this,
'Form',
$fields,
$actions,
$validator
);
$form->loadDataFrom($this->getFamilyMember());
return $form;
}

Avatar
JonoM

Community Member, 130 Posts

13 April 2011 at 1:00pm

I think you may need

new DropdownField('MemberTypeID', 'Member Type', $member_type_source)

Avatar
gregsaunders

Community Member, 12 Posts

13 April 2011 at 3:54pm

Dude, you rock! I'm toasting you with a nice glass of 08 Cote du Rhone as I write.

I can't believe I spent 1/2 a day on this one ... lesson learned. Come to forum earlier and ask politely.

Thank you
Greg

Avatar
mierla

Community Member, 16 Posts

15 April 2011 at 8:36am

@JonoM A tip of the hat to you, sir! I too had been fighting with this gnarly issue for longer than I care to admit. Thanks for making the stripey silver road a little smoother.