Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » DataObject has_one DropdownField

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

Page: 1
Go to End
Author Topic: 2285 Views
  • gregsaunders
    Avatar
    Community Member
    12 Posts

    DataObject has_one DropdownField Link to this post

    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;      
    }

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: DataObject has_one DropdownField Link to this post

    I think you may need

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

  • gregsaunders
    Avatar
    Community Member
    12 Posts

    Re: DataObject has_one DropdownField Link to this post

    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

  • mierla
    Avatar
    Community Member
    16 Posts

    Re: DataObject has_one DropdownField Link to this post

    @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.

    2285 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.