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