Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Bug? CheckboxField resets default values

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

Page: 1
Go to End
Author Topic: 2587 Views
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Bug? CheckboxField resets default values Link to this post

    I've been trying to set a default value for a CheckboxField, however this doesn't seem to behave the same way as TextareaField and Textfield. I've included a basic working sample below copied and pasted from bits from my actual form.

    The textarea and textfields show the default value initially. If I change them and submit the form the changed values are shown. If I uncheck CB1 and check CB2 and submit the form they changes are not retained and their values are reset. I would expect them to keep the latest settings as the default value should only be used the first time as with the other two text fields.

    Is it just me missing something or should I report this as a bug?

    function TheForm() {
       $fields = new FieldSet(
          new TextareaField('Textarea','Area', 8, 3, "Text area test"),
          new CheckboxField('CB1', 'Checked', 1),
          new CheckboxField('CB2', 'Not checked', 0),
          new TextField('TextField', 'Textfield test')
       );
       $actions = new FieldSet(
          new FormAction('ProcessForm', 'Process Form')
       );

       $form = new Form($this, 'MyForm', $fields, $actions, $validator);

       if(is_array(Session::get('MyForm'))) {
          $form->loadDataFrom(Session::get('MyForm'));
       }
       return $form;
    }

    function ProcessForm($data, $form) {
       Session::set("MyForm", $data);
       return $this->customise($data)->renderWith(array('MyForm', 'Page'));
       // Do form stuff
    }

    MyForm.ss

    $Textarea
    $CB1
    $CB2
    $Textfield
    $TheForm

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Bug? CheckboxField resets default values Link to this post

    I haven't tested this, but this is my guess:

    If the checkbox is ticked, a value of 'on' will be passed for that field.
    If the checkbox is not ticked, no value is passed.

    This means that if the checkbox is not ticked, you get no data (so any default will override it), and if the checkbox is ticked, the value is set as 'on', which is invalid.

    It is a bit of a flaw in the way 'loadDataFrom' works, so you might have to do something like:

    function ProcessForm($data, $form) {

       if(isset($data['CB1']))
          $data['CB1'] = 1;
       else
          $data['CB1'] = 0;

       if(isset($data['CB2']))
          $data['CB2'] = 1;
       else
          $data['CB2'] = 0;

       Session::set("MyForm", $data);
       return $this->customise($data)->renderWith(array('MyForm', 'Page'));
    }

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Bug? CheckboxField resets default values Link to this post

    Thanks for posting the workaround Hamish, I'll try it out asap.

    But to get a discussion going; shouldn't a CMS framework as SilverStripe handle just this type of exceptions internally? That's one of the points of having a framework isn't it, creating a wrapper for functions so they all behave in a similar. Then a developer doesn't have to manually program exceptions in processing different data types - they all should behave in the same manner with default values that can be passed automatically set.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Bug? CheckboxField resets default values Link to this post

    If it behaves as you say I would say its a bug but the fix for this would go a bit deeper as like Hamish said. Could you post a complete test file just so I can run through it quickly before you submit a ticket for it.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Bug? CheckboxField resets default values Link to this post

    Some more info - there is no error in SilverStripe:

    Firstly, checkbox fields will pass '1' if they're checked. My tests show that a checked box should load data correctly. Don't know why this isn't the case for you.

    Secondly, since unchecked checkboxes will not pass data, set the second argument of loadDataFrom to 'true'. This is a 'clear missing fields' argument that is false by default. Set it to true to clear checkboxes when the variable is not set (ie, the value is unchecked).

    $form->loadDataFrom(Session::get('MyForm'), true);

    On a side note, I've just found that listboxfields don't populate from dataobject many_many relations loaded this way . Might be a patch coming up.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Bug? CheckboxField resets default values Link to this post

    ...or maybe not. Code comments in Form->loadDataFrom:

    // We don't actually call the method because it might be slow.
    // In a later release, relation methods will just return references to the query that should be executed,
    // and so we will be able to safely pass the return value of the
    // relation method to the first argument of setValue

    *sigh*

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