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

How to validate email in an extended Member class with modelAdmin


Go to End


2 Posts   2155 Views

Avatar
Myrdhin

Community Member, 70 Posts

15 September 2010 at 4:59am

Edited: 15/09/2010 5:39am

Hello,

I have 2 classes : "ExtMember" and "ExtMemberAdmin"

+ ExtMember.php :

class ExtMember extends Member {

	static $db = array(
		'extName' => 'Varchar'
	);

}

+ ExtMemberAdmin.php :

class ExtMemberAdmin extends ModelAdmin {
	static $url_segment = 'extMember';

	static $managed_models = array(
		'ExtMember'
	);
}

When i add a new "ExtMember" member with an already registered email, this doesn't work : the "Add" button turns, turns and turns infinitely.

If i use Firebug to view the response, i have this :

ERROR [User Error]: Uncaught ValidationException: 
IN POST /silverstripe-v2.4.1/admin/extMember/ExtMember/AddForm?action_doCreate=Ajout
Line 611 in /var/www/silverstripe-v2.4.1/sapphire/security/Member.php

Source
======
  602: 				'Member', 
  603: 				sprintf(
  604: 					"\"%s\" = '%s' %s",
  605: 					$identifierField,
  606: 					Convert::raw2sql($this->$identifierField),
  607: 					$idClause
  608: 				)
  609: 			);
  610: 			if($existingRecord) {
* 611: 				throw new ValidationException(new ValidationResult(false, sprintf(
  612: 					_t(
  613: 						'Member.ValidationIdentifierFailed', 
  614: 						'Can\'t overwrite existing member #%d with identical identifier (%s = %s))', 
  615: 						PR_MEDIUM,
  616: 						'The values in brackets show a fieldname mapped to a value, usually denoting an existing email
       address'
  617: 					),

Trace
=====
Member->onBeforeWrite()
line 936 of DataObject.php

DataObject->write()
line 859 of ModelAdmin.php

ModelAdmin_CollectionController->doCreate(Array,Form,SS_HTTPRequest)
line 300 of Form.php

Form->httpSubmission(SS_HTTPRequest)
line 137 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 155 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 149 of Controller.php

Controller->handleRequest(SS_HTTPRequest)
line 155 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 149 of Controller.php

Controller->handleRequest(SS_HTTPRequest)
line 281 of Director.php

Director::handleRequest(SS_HTTPRequest,Session)
line 124 of Director.php

Director::direct(/admin/extMember/ExtMember/AddForm)
line 127 of main.php

I would like to know how to validate (only a unique email) "email" in a modelAdmin (like the email Member validation in Security Tab) ?

Thank you for your help,

Avatar
swaiba

Forum Moderator, 1899 Posts

15 September 2010 at 11:16pm

To add validation to a DataObject that is used in Model Admin I do the following... however for "Member" this is a special case and I would have decorated or used a custom class when using this instead of just extending it... for more info that see here... http://doc.silverstripe.org/member?s[]=member#extending_member_or_dataobject

<?php

class ExampleDataObject extends DataObject
{
	static $db = array(
		'Name' => 'Text'
	);
}

class DataObject_Validator extends RequiredFields
{
	function php($data)
	{
		$bRet = parent::php($data);

		if ($data['Name'] == 'Fred')
		{
			$obj->validationError(
				'Name',
				'Name must not be Fred',
				"required"
			);

			$bRet = false;
		}

		return $bRet;
	}
}