Jump to:

23380 Posts in 18307 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » How to validate email in an extended Member class with modelAdmin

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

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

Page: 1
Go to End
Author Topic: 1523 Views
  • Myrdhin
    Avatar
    Community Member
    67 Posts

    How to validate email in an extended Member class with modelAdmin Link to this post

    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,

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts

    Re: How to validate email in an extended Member class with modelAdmin Link to this post

    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;
       }
    }

    1523 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.