Jump to:

1971 Posts in 1275 Topics by 607 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: 1257 Views
  • golo
    Avatar
    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);

    $email->setTemplate('ContactEmail');

    $email->populateTemplate($data);

    $email->send();

    Director::redirect($this->Link("?success=1"));
    }
       
       public function Success()
    {
    return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
    }
       
    }

    thanx fpr helping

  • FungshuiElephant
    Avatar
    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) {
             $validator->validationError(
                $this->name,
                ($this->customValidationMessage)?$this->customValidationMessage:sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', strip_tags('"' . ($this->Title() ? $this->Title() : $this->Name()) . '"')),
                "required"
             );
          }

          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.

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