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

Is User Registration Possible?


Reply

6 Posts   3815 Views

Avatar
craig-oeuk

8 May 2009 at 12:38am Community Member, 1 Post

I'm struggling to find any documentation in regards to getting users to sign up.

There seems to be a bunch of security/authentication stuff but nothing which actually tells me how to set up users in a SilverStripe project?

Avatar
TerryMiddleton

8 May 2009 at 1:00am Community Member, 108 Posts

Oh yes, very easily.

You are going to create a new page (I called mine RegistrationForm.php) and save it in you mysite/code/RegistrationForm.php

See my RegistrationForm.php below.

Then you need to create a RegistrationForm.ss and save that in your project/templates/RegistrationForm.ss

The RegistrationForm.ss will look similar (if you want) to the Page.ss. The main variable you need in this page is $Form which is passed from the RegistrationForm.php

If you need to include the email notification like I did you will want to create a RegistrationFormEmail.ss that is used to format the email that gets sent.

Notice in the RegistrationForm.php there is a setTemplate that directs the page to use RegistrationFormEmail.ss as the template as well as the RegistrationForm.ss.

I hope this helps. You can see it in action if you would like at http://63.247.208.15/registration-request

Terry

RestrationForm.php
<?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
   private $defaultGroupID = 2;

   /**
   * 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 EmailField("Email", "Email address"),
         new PasswordField("Password", "Create a Password"),
         new TextField("FirstName", "First name"),
         new TextField("LastName", "Last name"),
         new TextField("Phone", "Phone"),
         new TextField("Company", "Company/Hospital"),
         new TextField("Address1", "Address 1"),
         new TextField("Address2", "Address 2"),
         new TextField("City", "City"),
         new TextField("State", "State"),
         new TextField("Zip", "Zip"),
         new TextField("Country", "Country"),
         new ReadonlyField("IPAddress","IPAddress",$_SERVER['REMOTE_ADDR'])
         

      ), new FieldSet(

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

      ), new RequiredFields(

         // List the required fields here: "Email", "FirstName"
         "Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"

      ));
      $Form->setTemplate('RegistrationForm');
   }

   /**
   * 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();

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

         $member->Groups()->add($group);
         // Redirect to a page thanking people for registering
         //Set data
            $From = $data['Email'];
            $To = "email address of who gets the email, 2nd email addres";
            $Subject = "Registration Request";
            $email = new Email($From, $To, $Subject);
            //set template
            $email->setTemplate('RegistrationFormEmail');
            //populate template
            $email->populateTemplate($data);
            //send mail
            $email->send();
         
         //$e = new Email();
         //$e->setTemplate("RegistrationForm");
         //$e->To = "terry@thestoreinsider.com";
         //$e->Subject = "Registration Request";
//         $e->Body = populateTemplate($data);

         //$e->Body = $member;
         //$e->send();
         Director::redirect('thanks-for-registering/');

      }else{

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

      }

   }
}

?>

Avatar
AdamJ

8 May 2009 at 2:39am (Last edited: 8 May 2009 2:40am), Community Member, 145 Posts

Terry, when you post code, its best to surround it with [ code ][ /code ] tags, it displays better and makes it easier to read

Avatar
TerryMiddleton

8 May 2009 at 2:59am Community Member, 108 Posts

Thanks for this. I will do this next time.

Terry

Avatar
rpw2

14 June 2009 at 10:12pm Community Member, 6 Posts

Hi Terry, thanks for the example. Can you explain more about how and where the additional member data fields are processed and saved (which table). By following the example I can save names, email and password (all fields are displayed in form) into existing member table and assign to a group but am I missing something here ($db?) to save additional fields??? Thanks.
nb: I have added a similar question to another post too.

Avatar
rpw2

18 June 2009 at 1:54pm Community Member, 6 Posts

More clues here;

http://www.silverstripe.org/customising-the-cms/show/251406#post251406

and here (this link is at bottom of above post);

this tutorial may be useful for you :
http://www.ssbits.com/custom-login-form-with-group-based-redirection/

Also, in one of the tutorials in suggests, if you want to just grab peoples details and not give them any 'logged in' capabilities, then don't register them (with a password) in the core members table.
The Forum module is perfect for seeing how to extend the current member table, if you do need to do it tho.