<![CDATA[ Classextender issue: New user registration (web context) not updating custom fields - My Forums]]> https://forums.modx.com/thread/?thread=96989 <![CDATA[Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-524744
However, my 'new user' Registration form is not saving the custom fields from the web context. The user registration form IS saved and accepted, and the user is created, only without the custom fields.

I defined some new custom fields in 'MyExtUserSchema', and now these functions are working, and custom field data is being saved:

1) From Manager: create/edit user (with custom fields)

2) From web context: user can update EXISTING Profile (with custom fields)

Here's a snippet from the [[$lgnRegisterForm]] chunk, with custom fields addeded:

[[!ExtUserUpdateProfile]]
[[!Register?
&submitVar=`loginRegisterBtn`
&activationResourceId=`52`
&activationEmailTpl=`lgnActivateEmailTplCustom`
&activationEmailSubject=`Thanks for Registering!`
&submittedResourceId=`51`
&usergroups=`Registered Users`
&validate=`nospam:blank,
username:required:minLength=^6^,
password:required:minLength=^6^,
password_confirm:password_confirm=^password^,
fullname:required,
email:required:email`
&placeholderPrefix=`reg.`
&useExtended=`0`
]]

<p>
<label for="username">[[%register.username? &namespace=`login` &topic=`register`]]</label>
<input type="text" name="username" value="[[!+reg.username]]" />
<span class="validateError">[[!+reg.error.username]]</span>
</p>

<p>
<label for="firstName">First Name</label>
<input type="text" name="firstName" value="[[!+reg.firstName]]" />
<span class="validateError">[[!+reg.error.firstName]]</span>
<p>

[...]

<input type="submit" name="loginRegisterBtn" class="btn-CSS" value="Register" />


I'd greatly appreciate any tips as to what to fix so that the custom fields also save!

Thanks in advance.]]>
kjforsyth Apr 22, 2015, 02:08 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-524744
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-537963
After a couple of hours experiencing exactly the problem described here I read this and it nudged me in the right direction.

I realised I had forgotten to add
&useExtended=`0`
to my Update Profile page.

Thank you Bob and Karl]]>
andytough Feb 06, 2016, 09:34 AM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-537963
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525193
It works great. Thank you so much for your help!

Karl]]>
kjforsyth May 05, 2015, 11:03 AM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525193
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields (Best Answer)]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525173

I'll add it to the next release (with credit to you, of course). Here it is with slightly modified error handling and other minor changes. Let me know if it still works. wink

$submission = isset($_POST['loginRegisterBtn']) && ($_POST['loginRegisterBtn'] == 'Register');

$data = NULL;
$newUser = NULL;
$userName = NULL;
$userID = NULL;
$fields = array();

/* @var $data userData */

if (isset($modx->user) && ($modx->user instanceof modUser)) {

    $userName = $hook->getValue('username');

   /* Get new user ID via username */
    $newUser = $modx->getObject("modUser", array('username' => $userName));
    $userId = $newUser->get('id');


    $data = $modx->getObject('userData',
        array('userdata_id' => $userId), false);
    if ($data) {
        $fields = $data->toArray();
    } else {
        $data = $modx->newObject('userData');
        if ($data) {
            $data->set('userdata_id', $userId);
            $fields = $data->toArray();
        }
    }
}

if (!is_array($fields) || empty($fields)) {
    $hook->addError('username', '[ExtUserRegisterPosthook] Error getting user data');
    return false;
}

/* Convert any nulls to '' */
if (!empty($fields)) {
    foreach ($fields as $key => $value) {
        if (empty($value) && ($value !== '0')) {
            $fields[$key] = '';
        }
    }
    $modx->setPlaceholders($fields);
}

if ($submission) {
    $modx->request->sanitizeRequest();
    $dirty = false;

    foreach ($fields as $key => $value) {
        if (isset($_POST[$key])) {
            if ($value !== $_POST[$key]) {
                $data->set($key, $_POST[$key]);
                $dirty = true;
            }
        }
    }

    if ($dirty) {
        if ($data->save()) {
            // $msg = '[ExtUserRegisterPosthook] Saved successfully';
            // $modx->log(modX::LOG_LEVEL_ERROR, $msg);
        } else {
            $msg = '[ExtUserRegisterPosthook] Save failed';
            $hook->addError('username', $msg);
            return false;
        }
    }
}

return true;
]]>
BobRay May 05, 2015, 12:14 AM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525173
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525164
Thanks for your help sorting this out. Here's the entire working 'ExtUserRegister' snippet, which is based on your 'ExtUserUpdateProfile' snippet:


//$modx->lexicon->load('login:updateprofile');

$submission = isset($_POST['loginRegisterBtn']) && ($_POST['loginRegisterBtn'] == 'Register');

$data = null;
$newUser = null;
$userName = null;
$userID = null;
$fields = array();

/* @var $data userData */

if (isset($modx->user) && ($modx->user instanceof modUser)) {

$userName = $hook->getValue('username');

//START GET NEW USER ID:
$newUser = $modx->getObject("modUser",array('username' => $userName));
$userID = $newUser->get('id');
//END GET NEW USER ID

$data = $modx->getObject('userData',
array('userdata_id' => $userID), false);
if ($data) {
$fields = $data->toArray();
} else {
$data = $modx->newObject('userData');
if ($data) {
$data->set('userdata_id', $userID);
$fields = $data->toArray();

}
}
}

if (! is_array($fields) || empty($fields)) {
return '';
}

/* Convert any nulls to '' */
if (!empty($fields)) {
foreach($fields as $key => $value) {
if (empty($value) && ($value !== '0')) {
$fields[$key] = '';
}
}
$modx->setPlaceholders($fields);
}

if ($submission) {
$modx->request->sanitizeRequest();
$dirty = false;

foreach ($fields as $key => $value) {
if (isset($_POST[$key])) {
if ($value !== $_POST[$key]) {
$data->set($key, $_POST[$key]);
$dirty = true;
}
}
}

if ($dirty) {

if( $data->save()) {
$msg = '[ExtUserRegister] Saved successfully';
$modx->log(modX::LOG_LEVEL_ERROR, $msg);
} else {
$msg = '[ExtUserRegister] Save failed';
$modx->log(modX::LOG_LEVEL_ERROR, $msg);
}
}
}

return true;]]>
kjforsyth May 04, 2015, 05:00 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525164
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525161
$usr = $modx->getObject("modUser",array('username' => $userName));


How are you getting the value of the $data variable?

I think step 5 is necessary, otherwise Register won't run any subsequent hooks and can't forward the user.
]]>
BobRay May 04, 2015, 04:35 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525161
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525156
1) Used &postHooks=`ExtUserRegister`:

[[!Register?
&submitVar=`loginRegisterBtn`
...
&useExtended=`0`
&postHooks=`ExtUserRegister`
]]


2) In the 'ExtUserRegister' snippet, retrieved 'username':

$userName = $hook->getValue('username');


3) Looked up 'id' using 'username':

$c = $modx->newQuery('modUser');
$users = $modx->getCollection("modUser",$c);

foreach($users as $newUser){
if ($userName == $newUser->get('username')) {
$userID = $newUser->get('id');
break;
}
}


4) Set 'userdata_id' to 'id':

$data = $modx->getObject('userData',
array('userdata_id' => $userID), false);
if ($data) {
$fields = $data->toArray();
} else {
$data = $modx->newObject('userData');
if ($data) {
$data->set('userdata_id', $userID);
$fields = $data->toArray();
}
}


5) Modify return value from '' to true (I don't know if this was necessary)


NOTE: In step 3, with just a few users, it works fine and quickly to iterate through them to get the 'id', but might there be a faster way to get the user id, for when there are several hundred or thousand users to select from?]]>
kjforsyth May 04, 2015, 12:01 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525156
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525134 ]]> BobRay May 03, 2015, 11:52 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525134 <![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525133
Best wishes. Karl]]>
kjforsyth May 03, 2015, 11:34 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=3#dis-post-525133
<![CDATA[Re: Classextender issue: New user registration (web context) not updating custom fields]]> https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=2#dis-post-525112
The second thing is that I expected that the userdata_id would be set to 0, but I think it's being set to 1 because you're logged into the Manager and that's your ID. I could be wrong.

A possible solution is to run the snippet as a Register postHook (possibly with &persistParams=`1`), but it would have to be rewritten to get the data with $hook->getValue(). I think it would work, but I'm not sure. Unfortunately, I don't have time to mess with it right now.

It might also be possible to put the snippet on the "thanks for registering" page with &persistParams=`1`. At that point the user should exist, but I'm not sure what &persistParams actually does.

Remember that my original snippet is designed to update an existing user's profile, so $modx->user would always give access to the user's ID.

BTW, the record not being deleted when you delete a user is normal because the modUser object doesn't know about the custom table. The record has to be removed in a plugin attached to OnUserRemove. I think this would be the code (assuming that the userdata_id fields are correct):

$userId = $user->get('id');
$extUser = $modx->getObject('userData',array('userdata_id' => $userId));
if ($extUser) {
   $extUser->remove();
}
]]>
BobRay May 02, 2015, 06:03 PM https://forums.modx.com/thread/96989/classextender-issue-new-user-registration-web-context-not-updating-custom-fields?page=2#dis-post-525112