Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » ss 2.4.2 RequiredFields CheckboxField

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

Page: 1
Go to End
Author Topic: 1332 Views
  • golo
    Community Member
    1 Post

    ss 2.4.2 RequiredFields CheckboxField Link to this post

    Hi Forum,
    i build an simle Contactform with some required fields - it works fine exapt of the CheckboxField - it need to be checked.
    How can i do this?

    Here my code (part of):

    class ContactPage_Controller extends Page_Controller
    function ContactForm() {

    $fields = new FieldSet(
       new CheckboxField('Datenschutz', _t('ContactForm.datenschutzklausel'),0),
    new TextField('Name', 'Ihr Name*:'),
    new EmailField('Email', 'Ihre E-Mail*:'),
    new TextareaField('Comments','Nachricht an uns:')

    $actions = new FieldSet(
    new FormAction('SendContactForm', 'Send')

    $validator = new RequiredFields(array('Name', 'Email', 'Datenschutz'));

    return new Form($this, 'ContactForm', $fields, $actions, $validator);
        function SendContactForm($data, $form) {

    $From = $data['Email'];
    $To = $this->Mailto;
    $Subject = "Mediapool-Login beantragen";
    $email = new Email($From, $To, $Subject);




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

    thanx fpr helping

  • FungshuiElephant
    Community Member
    57 Posts

    Re: ss 2.4.2 RequiredFields CheckboxField Link to this post

    Another old post, but for others who need the same...

    Subclass Checkboxfield and write your own validate method:

    * Single checkbox field that must be ticked.
    * @package forms
    * @subpackage fields-basic
    class RequiredCheckboxField extends CheckboxField {

        * Validate (i.e. see whether it's checked or not.
        * @param validator
        * @return boolean Whether the field validates or not.
       public function validate($validator){
          $valid = $this->value==1;
          //Report error?
          if (!$valid) {
                ($this->customValidationMessage)?$this->customValidationMessage:sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', strip_tags('"' . ($this->Title() ? $this->Title() : $this->Name()) . '"')),

          return $valid;

    then just use RequiredCheckboxField in place of CheckboxField in your form.

    Works with a RequiredFields validator.
    You can use $myField->setcustomValidationMessage("message") to set a custom message.

    Aram's http://archive.ssbits.com/using-jquery-for-form-validation/
    about jQuery validation stuff might also be of interest.

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.