Jump to:

23358 Posts in 17875 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] OptionsetField not saving, please help

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2408 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    [SOLVED] OptionsetField not saving, please help Link to this post

    Help! I've built a form in the latest release of SS and now I'm having trouble saving the information where I have 'Optionsetfield', all the other field types save perfectly ok.

    I've used these before and not encountered this so I guess I'm just missing something here but on reading the documentation I can't spot anything.

    Here is my code:

       public function ResultsEditForm(){

          $urlAction = Director::URLParam('Action');
          $urlID = Convert::raw2sql(Director::urlParam('ID'));

          if(is_numeric($urlID)){
             $Contact = $caseInfo->Contact;
          }

          // Create fields for edit form
          $fields = new FieldSet(

                new OptionsetField(
                $name ='Contact',
                $title = 'Contact',
                $source = array(
                   "1" => "Yes",
                   "2" => "No"
                ), $value = $Contact)
          );
          $actions = new FieldSet(
             new FormAction("UpdateCase", "Update")
          );
          return new Form($this, "ResultsEditForm", $fields, $actions);
          }

        // Function to write our data
        public function UpdateCase($data, $form){
          $id = (int)$_REQUEST['CaseID'];
          $caseInfo = DataObject::get_by_id('myFifth', $Case->myFifthID);
          $caseInfo->Contact = $data['Contact'];
          $caseInfo->write();
    }
          Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
       }

       public function Success(){
          return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
       }

    I think I've included everything above, I've not posted the other fields as this post would be very very long.

    I've tried making the OptionsetField a DropdownField to see how they differ and it made no difference when saving.

    I'm close to a deadline here so quite anxious, this is the only thing holding me back, any help much appreciated, thanks.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    a) have you Debug:show($data) in UpdateCase? this will confirm that the code is called and 'Contact' has the right value to place there. For eaxmplae hardcoding the 'Contact' in that function might work, but the value in $data might be wrong...

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    Hi Swaiba,

    Thanks for your reply, yes I had debugged and the correct value seemed to be showing, it struck me that maybe the form is not saving the optionsetfields for some strange reason because if I change the value in the database via phpMyAdmin and then refresh my page it picks up the value ok, only each time I save it, it default to a '1' boolean value.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    You mean that you select "Yes", that maps to "1" and it saves that is the field? is that right?

    Maybe you need to have...

    $source = array(
    "Yes" => "Yes",
    "No" => "No" );

    what does you DO look like?

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    I think the way I have it is correct, basically if you select "Yes" it would be a value of '1' and if you select "No" it would be a value of '2'.

    Regardless of what you select it will save it as a value of 1, even with the code you have suggested.

    The code looks ok acording to the documentation also, is it possible this could be a bug?

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    What does you DataObject 'myFifth' look like?

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    See the attached image

    Attached Files
  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: [SOLVED] OptionsetField not saving, please help Link to this post

    yeah looks fine, but maybe this would help... changing...

    $id = (int)$_REQUEST['CaseID'];
    $caseInfo = DataObject::get_by_id('myFifth', $Case->myFifthID);
    $caseInfo->Contact = $data['Contact'];
    $caseInfo->write();

    to...

    $caseInfo = DataObject::get_by_id('myFifth', (int)$_REQUEST['CaseID']);
    $caseInfo->Contact = $data['Contact'];
    $caseInfo->write();

    2408 Views
Page: 1 2
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.