@benni91
This is what i have, its likely a bit more than what your after, but it might help.
In my Page Controller:
function SubscribeForm() {
$email ='';
$newsletter_group = DataObject::get_one('Group', "ID = 4");
$member = $this->CurrentMember();
if($member):
$email = $member->Email;
endif;
if(!$member || !$member->inGroup($newsletter_group)):
// Create Fields
$fields = new FieldSet(new EmailField('Email','Email', $email,'100'));
// Create Action
$actions = new FieldSet(new FormAction('doSubscribeForm', 'Go'));
// Create Validators
$validator = new RequiredFields('Email');
//Create Form
$form = new Form($this, 'SubscribeForm', $fields, $actions, $validator );
return $form;
endif;
}
function doSubscribeForm($data, $form){
$email = Convert::raw2sql($data['Email']); // prevent sql injection
$existing_member = DataObject::get_one("Member", "Email = '$email'");
if($member = $this->CurrentMember()):
// Get member with matching email.
// If logged in user matches that member add them to newsletter group.
if($member->ID == $existing_member->ID):
$newsletter_group = DataObject::get_one('Group', "ID = 4");
$existing_member->Groups()->add($newsletter_group);
endif;
return Director::redirectBack();
else:
if($existing_member):
return Director::redirect(Director::BaseURL() . 'Security/login');
else:
return Director::redirect($this->Link("/sign-up/?s=1&e=" . $email));
endif;
endif;
}
Template:
<div class="SubscribeForm">
<% if SubscribeForm %>
$SubscribeForm
<% else %>
<div> You've subscribed! </div>
<% end_if %>
</div>