For more information on forms, see the form page.
Form syntax is quite complex, so you'll need to know a little bit of PHP, or be willing to tinker with it to get things going. There are two main aspects to forms in Silverstripe:
The following code is all you need to get a basic signup form on your page. I recommend placing a file like this in <mysite>/code/RegistrationForm.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 TextField("FirstName", "First name"), new TextField("Surname"), new EmailField("Email", "Email address") ), new FieldSet( // List the action buttons here new FormAction("SignupAction", "Sign up") ), new RequiredFields( // 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(); // Add the member to group. (Check if it exists first) if($group = DataObject::get_one('Group', "ID = $this->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/'); } } }
Using the above code, you will need to adjust the $defaultGroupID variable to a group you wish the user to be added to by default. You may have the user automatically added to your newsletter group, for instance.
Finding the group ID is a little obscure, but not too difficult.
Be careful not to add add users to the administrative group (ID 1) by accident.
See CompositeField
see dataobject and datamodel
Sometimes, you'll want a page to show different forms depending on which action you're using. For example, you could have mysite/register/ and them mysite/register/step2 on a 2-step registration process. You can't exactly define 2 different methods both called Form(), and it would be a pain to create a different tempalte
class RegistrationForm_Controller extends Page_Controller { function Form() { // ... you already made this ... } function doform($data, $form) { // ... you already made this ... } function Step2Form() { return new Form($this, "Step2Form", new FieldSet( // ... you can copy the form from elsewhere } }
The UserDefinedForm page type provides capabilities for editing simple forms within the CMS. This can save a lot of iteration time.
class RegistrationForm_Controller extends Page_Controller { function doform($data, $form) { // your form actions FormResponse::update_dom_id('myForm', '<form>myformcontent</form>'); FormResponse::status_message('Done', 'good'); // will automatically show the status-message if called by ajax, or redirect on a normal HTTP-request return FormResponse::respond() } }
Please use comments for notes, tips and corrections about the described
functionality.
Use the Silverstripe Forum to
ask questions.