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.

General Questions /

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

[SOLVED] OptionsetField not saving, please help


Go to End
Reply


12 Posts   2601 Views

Avatar
Harley

Community Member, 153 Posts

8 January 2011 at 4:36am

Edited: 08/01/2011 4:37am

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.

Avatar
swaiba

Forum Moderator, 1804 Posts

8 January 2011 at 5:26am

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

Avatar
Harley

Community Member, 153 Posts

8 January 2011 at 5:29am

Edited: 08/01/2011 5:33am

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.

Avatar
swaiba

Forum Moderator, 1804 Posts

8 January 2011 at 5:33am

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?

Avatar
Harley

Community Member, 153 Posts

8 January 2011 at 5:38am

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?

Avatar
swaiba

Forum Moderator, 1804 Posts

8 January 2011 at 5:39am

What does you DataObject 'myFifth' look like?

Avatar
Harley

Community Member, 153 Posts

8 January 2011 at 5:42am

See the attached image

Attached Files
Avatar
swaiba

Forum Moderator, 1804 Posts

8 January 2011 at 5:46am

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();

Go to Top