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

Form with required checkbox


Go to End
Reply


13 Posts   2989 Views

Avatar
benh

Community Member, 4 Posts

24 November 2010 at 11:20am

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

Avatar
Willr

Forum Moderator, 5513 Posts

24 November 2010 at 5:34pm

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.

Avatar
benh

Community Member, 4 Posts

25 November 2010 at 1:23am

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

Avatar
benh

Community Member, 4 Posts

25 November 2010 at 11:04pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

26 November 2010 at 3:52pm

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

Avatar
biapar

Forum Moderator, 435 Posts

27 November 2010 at 2:54am

I use jquery.validator and I disable prototype.

Avatar
Silverfish

Community Member, 59 Posts

25 November 2011 at 4:28am

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

Avatar
Willr

Forum Moderator, 5513 Posts

25 November 2011 at 9:02pm

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

Go to Top