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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[solved] Newsletter subscription error on existing user email


Go to End


4 Posts   1110 Views

Avatar
Pipifix

Community Member, 56 Posts

10 February 2012 at 4:31am

Hi everybody.

On bugfixin our latest project we discovered a strange newsletter subscription error. On the homepage there is a form saving all subscribers as a new member in the db. if a mailadress is already saved – no matter whether teh user is an admin or in another usergroup – a exception is thrwon.

throw new ValidationException(new ValidationResult(false, sprintf(
629 _t(
630 'Member.ValidationIdentifierFailed',
631 'Can\'t overwrite existing member #%d with identical identifier (%s = %s))',
632 PR_MEDIUM,
633 'The values in brackets show a fieldname mapped to a value, usually denoting an existing email address'
634 ),

The code on Homepage.php

// Make sure you set this to the right group.
	// See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
	private $defaultGroupID = 3;
 
	/**
	* This function lets you put a form on your page, using $Form.
	*/
	 function NewsletterForm() {
		return new Form($this, "NewsletterForm", new FieldSet(
 
			// List your fields here
			new TextField("Surname", "Name"),
			new EmailField("Email", "E-Mail")
 
		), new FieldSet(
 
			// List the action buttons here
			new FormAction("SignupAction", "Eintragen")
 
		), new RequiredFields(
			'Email',
			'Surname'	 
		));
	}

	/**
	* 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('newsletter-erfolgreich-eingetragen/');
 
		}else{
			// Redirect to a failure page
			Director::redirect('newsletter-registristrierungs-fehler/');
 
		}
 
	}

$defaultGroupID = 3 is the newsletter user group created in the security section of the cms.
Why does the Director::redirect('newsletter-registristrierungs-fehler/'); not work?
What to edit on this code to catch this error?

Thanks. Pipifix

Silverstripe 2.46

Avatar
Devlin

Community Member, 344 Posts

10 February 2012 at 5:09am

What to edit on this code to catch this error?

You will need to validate the mailadress yourself. Silverstripe will just throw the error, because double user identifier is a bad thing.

Why does the Director::redirect('newsletter-registristrierungs-fehler/'); not work?

Because you check in your if clause if a group with the ID 3 exists. Which of course is always the case since you created it.

Avatar
Pipifix

Community Member, 56 Posts

10 February 2012 at 6:38am

Thanks Devlin.

The double user issue was clear. See the thread headline ;) The second things was too obviously. ^^
You're right. This condition is always true. My problem is i'm a php-noob. I need a lot more programming exercise to write creative and useful code. The most of the code provided here, im able to read and understand. Thats why i ask for a little coding help.

I found this snippet of code http://www.silverstripe.org/all-other-modules/show/15228?start=8#post311707 and tried to adapt it. This checkt the user and sanitize the mailadress. But this piece dont write in the usergroup and the redirection is something weird. (404) I like the idea to set the NewsletterSubscriber as a 'quiet' user and redirect them to a sucess-page or a failure-page. No login should be seen.

Thanks again. Pipifix.

Avatar
Pipifix

Community Member, 56 Posts

14 February 2012 at 7:05am

[solved]

Hello verybody , hello Devlin.

I found a snippet/tutorial that do exactly what i want so i wana share this with you.
http://www.clickheredigital.co.uk/blog/how-to-include-a-silverstripe-form-on-any-every-page/

happy coding. Pipifix