Jump to:

23551 Posts in 19346 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » Form with required checkbox

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: 2924 Views
  • benh
    Avatar
    Community Member
    4 Posts

    Form with required checkbox Link to this post

    Hi there,

    I'm an SS nube trying to create a form with a checkbox that the user has to check before submitting the form. I'm sure its pretty easy but nothing I've done so far seems to work. Here's what I tried so far (stripped down to the minimum for clarity):

    class Test_Form extends Form {
       public function __construct($controller, $name) {
          $fields = new FieldSet(new ConsentField('TsAndCs', 'I agree to the Ts & Cs'));
          
          //ACTIONS
          $actions = new FieldSet(
             new FormAction('submit', 'Register')
          );
          
          //VALIDATOR
          $validator = new RequiredFields('TsAndCs');
          
          parent::__construct($controller, $name, $fields, $actions, $validator);
       }
    }

    class ConsentField extends CheckboxField {
       function validate($validator) {
          return $this->value == 1 ? true : false;
       }
    }

    It appears on the form but the form submits regardless... can anyone switch on the light please?! Also how would I add javascript validation for this as well?

    Many thanks

    Ben

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Form with required checkbox Link to this post

    Pretty sure you can simply make a checkbox field required.

    $fields = new FieldSet(new CheckboxField('TsAndCs', 'I agree to the Ts & Cs'));

    $actions = new FieldSet(new FormAction('submit', 'Register'));

    $validator = new RequiredFields(array('TsAndCs'));

    parent::__construct($controller, $name, $fields, $actions, $validator);

    The validator should pick up the TsAndCs box.

  • benh
    Avatar
    Community Member
    4 Posts

    Re: Form with required checkbox Link to this post

    Thanks for the feedback Will.

    Unfortunately that doesn't seem to work. The form submits whether the checkbox is checked or not.

    While debugging the values of the fields I see that the checkbox is getting its value set to "0" when it is unchecked. This happens before any other form processing occurs (but I'm not sure exactly how this occurs yet?).

    I looked in the RequiredFields line 111, in the php() method that gets called when the form is submitted, and I saw that the required test for each of the (non-upload) required fields is like this

    $error = (strlen($value)) ? false : true;

    So the unchecked checkbox value="0" sets $error to false, when I need it to return true.

    If I do this in RequiredFields->php() instead:

    if ($formField == "ConsentField"){
    $error = ($value == "1") ? false : true;
    } else {
    $error = (strlen($value)) ? false : true;
    }

    Then it works.

    Is there a way, perhaps in my ConsentField class that I can prevent the field getting set to "0" when it is unchecked maybe?

    Thanks,

    Ben

  • benh
    Avatar
    Community Member
    4 Posts

    Re: Form with required checkbox Link to this post

    Hi again,

    Sorry to pester, but please can anyone confirm whether this is a problem with my understanding or a problem with the framework? I have a big deadline looming (and the first publication of a Silverstripe site for our company) and I need to get this working.
    Just to reiterate the functionlaity required is just a checkbox that the user has to check before submitting the form (e.g. I agree to the Ts&Cs).

    Not sure if this is relevant, but we have our form in it's own class, extending Form, as opposed to a function that returns a new form().

    Please note: simply adding a basic checkbox to the form and adding it to the RequiredFields array does not work.

    Many thanks,
    Ben

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Form with required checkbox Link to this post

    This is a bug with the framework. A ticket has been raised http://open.silverstripe.org/ticket/6231

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: Form with required checkbox Link to this post

    I use jquery.validator and I disable prototype.

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: Form with required checkbox Link to this post

    Hi Willr,

    Hmm.. almost exactly one year later, working with 2.4.6 and the Bug still exists.

    May I kindly ask if there's a plan when to fix it?

    If I would fix it and send you the code, would you be interested?

    Regards
    SF

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Form with required checkbox Link to this post

    It's in 3.0 as mentioned on - http://open.silverstripe.org/ticket/6231

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