Jump to:

1970 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Checkboxes and workflow...

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

Page: 1
Go to End
Author Topic: 877 Views
  • Puppy
    Avatar
    Community Member
    10 Posts

    Checkboxes and workflow... Link to this post

    Hi guys,

    I am really enjoying using Silverstripe and having fun learning to customise but I have run into a small problem while playing with the tutorial form.

    <?php

    class RegistrationForm extends Page {
    }

    class RegistrationForm_Controller extends Page_Controller {

       // Make sure you set this to the right group.
       // See http://doc.silverstripe.com/doku.php?do=show&amp;id=recipes%3Aforms

       /**
       * This function lets you put a form on your page, using $Form.
       */
       function Form() {
          return new Form($this, "Form", new FieldSet(

             // List your fields here
             new TextField("FirstName", "First name"),
             new TextField("Surname"),
             new EmailField("Email", "Email address"),
             new CheckboxField(
             $name = "Private",
             $title = "I would like to remain private")
             
          ), new FieldSet(

             // List the action buttons here
             new FormAction("SignupAction", "Sign up")

          ), new RequiredFields(
                   "FirstName", "Email", "Surname"
             // List the required fields here: "Email", "FirstName"

          ));
       }

       /**
       * This function is called when the user submits the form.
       */
       function SignupAction($data, $form) {
       
       if(isset($form->Private)){
          // Create a new Member object and load the form data into it
          $member = new Member();
          $form->saveInto($member);
          $defaultGroupID = 6;

          // Write it to the database. This needs to happen before we add it to a group
          $member->write();

          // Add the member to group. (Check if it exists first)
          if($group = DataObject::get_one('Group', "ID = $defaultGroupID")) {

             $member->Groups()->add($group);
             // Redirect to a page thanking people for registering
             Director::redirect('thanks-for-your-support/');

          }else{

             // Redirect to a failure page
             Director::redirect('registration-failed/');

          }
       }
       else{
       // Create a new Member object and load the form data into it
          $member = new Member();
          $form->saveInto($member);
          $defaultGroupID = 5;

          // Write it to the database. This needs to happen before we add it to a group
          $member->write();

          // Add the member to group. (Check if it exists first)
          if($group = DataObject::get_one('Group', "ID = $defaultGroupID")) {

             $member->Groups()->add($group);
             // Redirect to a page thanking people for registering
             Director::redirect('thanks-for-registering/');

          }else{

             // Redirect to a failure page
             Director::redirect('registration-failed/');

          }
       }

       }
    }
    ?>

    I've relentlessly searched the forums/documentation and tried numerous ways to have a checkbox define the behaviour of the form with an if statement.

    I have a category for 'supporters' (4) which has two sub-categories for Public (5) and Private (6). (This will be useful when I need to display supporters of our website as they are able to opt out of public listings. Though I would still be able to show all in admin pages using group (4). )

    All works reasonably well but it is refusing to check if the checkbox has been selected therefore routing everyone into Public (5) by the else statement.

    if(isset($form->Private))

    I was playing around with the position of the member class which is created before but I have tried other alternatives such as POST.

    I would be really grateful if someone could point me into the right direction.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Checkboxes and workflow... Link to this post

    Doesn't isset($form->Private) check if the var is set? So regardless if true or false it'll be set wont it?
    Maybe check ($form->Private==1)

  • Puppy
    Avatar
    Community Member
    10 Posts

    Re: Checkboxes and workflow... Link to this post

    Yep you're right ! With problems I had before I had overlooked $member instead of the $form I needed, my bad!

    Solved this problem with ($member->Private==1)

    <?php
    class RegistrationForm extends Page {
    }

    class RegistrationForm_Controller extends Page_Controller {

       // Make sure you set this to the right group.
       // See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
       /**
       * This function lets you put a form on your page, using $Form.
       */
       function Form() {
          return new Form($this, "Form", new FieldSet(

             // List your fields here
             new TextField("FirstName", "First name"),
             new TextField("Surname"),
             new EmailField("Email", "Email address"),
             new ConfirmedPasswordField("Password"),
             new CheckboxField("Private", "Please tick to remain private")

          ), new FieldSet(

             // List the action buttons here
             new FormAction("SignupAction", "Sign up")

          ), new RequiredFields(
             "FirstName", "Email", "Surname"
             // List the required fields here: "Email", "FirstName"

          ));
       }
       
       /**
       * This function is called when the user submits the form.
       */
       function SignupAction($data, $form) {

          // Create a new Member object and load the form data into it
          $member = new Member();
          $form->saveInto($member);
          
             // Write it to the database. This needs to happen before we add it to a group
             $member->write();
             
             // Check if private setting is on
             if($member->Private==1){
                $privGroupID = 6;
                // Add the member to group. (Check if it exists first)
                if($group = DataObject::get_one('Group', "ID = $privGroupID")) {
                   $member->Groups()->add($group);
                   // Redirect to a page thanking people for registering
                   Director::redirect('thanks-for-registering-privately/');
          
                }else{
                   // Redirect to a failure page
                   Director::redirect('registration-failed/');
                }
             }else{
                $defaultGroupID = 5;
                // Add the member to group. (Check if it exists first)
                if($group = DataObject::get_one('Group', "ID = $defaultGroupID")) {
                   $member->Groups()->add($group);
                   // Redirect to a page thanking people for registering
                   Director::redirect('thanks-for-registering/');
          
                }else{
                   // Redirect to a failure page
                   Director::redirect('registration-failed/');
                }
             }
          
          

       }
    }?>

    Though, I am having problems building an email check with get_one()

    if($member = DataObject::get_one('Member', "Email = '". Convert::raw2sql($data['Email']) ."'"))


    OR

    $email = Convert::raw2sql($data['Email']);

    if($member = DataObject::get_one('Member', "Email = $email"))

    Would it be sensible to use the $member variable again or does something look wrong to anyone?

  • Puppy
    Avatar
    Community Member
    10 Posts

    Re: Checkboxes and workflow... Link to this post

    Found a better method for checking the database to see if email exists.

    <?php
    class RegistrationForm extends Page {
    }

    class RegistrationForm_Controller extends Page_Controller {

       // Make sure you set this to the right group.
       // See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
       /**
       * This function lets you put a form on your page, using $Form.
       */
       function Form() {
          return new Form($this, "Form", new FieldSet(

             // List your fields here
             new TextField("FirstName", "First name"),
             new TextField("Surname"),
             new EmailField("Email", "Email address"),
             new ConfirmedPasswordField("Password"),
             new CheckboxField("Private", "Please tick to remain private")

          ), new FieldSet(

             // List the action buttons here
             new FormAction("SignupAction", "Sign up")

          ), new RequiredFields(
             "FirstName", "Email", "Surname"
             // List the required fields here: "Email", "FirstName"

          ));
       }
       
       /**
       * This function is called when the user submits the form.
       */
       function SignupAction($data, $form) {

          // Create a new Member object and load the form data into it
          $member = new Member();
          $form->saveInto($member);
          
          
          if(DataObject::get_one('Member', "Email = '".$member->getField('Email')."'")) {
          // Redirect to a failure page
          $form->addErrorMessage("Email", 'Sorry, the email you have entered is already registered.', "bad");
          
             Session::set("FormInfo.Form_Form.data", $data);
             
             return Director::redirectBack();
          
          }else{      
             // Write it to the database. This needs to happen before we add it to a group
             $member->write();
             
             // Check if private setting is on
             if($member->Private==1){
                $privGroupID = 6;
                // Add the member to group. (Check if it exists first)
                if($group = DataObject::get_one('Group', "ID = $privGroupID")) {
                   $member->Groups()->add($group);
                   // Redirect to a page thanking people for registering
                   Director::redirect('thanks-for-registering-privately/');
          
                }else{
                   // Redirect to a failure page
                   Director::redirect('registration-failed/');
                }
             }else{
                $defaultGroupID = 5;
                // Add the member to group. (Check if it exists first)
                if($group = DataObject::get_one('Group', "ID = $defaultGroupID")) {
                   $member->Groups()->add($group);
                   // Redirect to a page thanking people for registering
                   Director::redirect('thanks-for-registering/');
          
                }else{
                   // Redirect to a failure page
                   Director::redirect('registration-failed/');
                }
             }
          }
          
          

       }
    }?>

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