[UpdateProfile + ClassExtender] Data updated, even if custom validation fails

Hi everyone,

I use the UpdateProfile tag:


And a custom validation snippet:

if ( $value < 100 ){
    $validator->addError($key,'The value should be at least 100');
return '';

This value is an extended field created with ClassExtender, not the native modx extended field.

After validation, the error message is properly showing up however the data is updated in the database even when it should not be saved (for example: if the value is “50” it should not be saved)

I also have the “ExtUserUpdateProfile” snippet but I don’t know how can I bypass the “save” in this snippet?

    if ($dirty) {

Can you help me? :slight_smile:

I think this prevent the DB update, but I’m not sure if the error message will appear:

if ($dirty) {
       if ((int) $_POST['fieldName'] >= 100) {
        } else {
            $modx->setPlaceholder('value_error', 'The value should be at least 100' );

Change $_POST[‘fieldName’] to the actual name of your field and put the following placeholder where you want the error message to appear:


I think the placeholder will have to be below the snippet tag.

Another method would be to use JavaScript to catch the error, prevent the form from being submitted, and pop up an error message.

Many thanks @bobray!

I’m glad that worked for you. :slight_smile:

Thanks for reporting back.

Shouldn’t the validation snippet just return false to stop execution/updating?

I think the validation snippet is ok and the problem is just the combination of the snippets ExtUserUpdateProfile and UpdateProfile


ExtUserUpdateProfile is always executed and doesn’t care if the validation in the snippet UpdateProfile fails. It would probably be better to create a hook-snippet for UpdateProfile to update the custom user data.

Yes you are right the combination of both snippets is problematic.

Yesterday I decided to totally remove the “UpdateProfile” snippet, i think it would be easier to have just one snippet to manage all the fields.

And it’s the “ExtUserUpdateProfile” who should take care of the validation and who should save the updated data.

The snippet should work but unfortunately it only saves the fields related to “ClassExtender”, the native modx fields (email, phone,website…) are not saved. Maybe I have to change or add something in this part of the code ?

 $user =& $modx->user;
 $data = $modx->getObject('userData', array('userdata_id' => $user->get('id')), false);
 $fields = $data->toArray();
 print_r($fields);  // This line only gives the extended fields in the ClassExtender table

I probably would have tried to intregrate the saving part into “UpdateProfile” with a hook snippet, but if you want “ExtUserUpdateProfile” to handle everything, you would need code like this. (Check the code of the other snippets like SetUserPlaceholders for reference)

$user =& $modx->user;
$c = $modx->newQuery('userData');
$c->where(array('userdata_id' => $user->get('id')));
$data = $modx->getObjectGraph('userData', '{"Profile":{},"User":{}}', $c);
$fields = $data->toArray();
if ($data->Profile) {
	$fields = array_merge($data->Profile->toArray(), $fields);
if ($data->User) {
	$fields = array_merge($data->User->toArray(), $fields);

Hi, yes I had this portion of code, it fetches and displays properly all the field, however it doesn’t save the native modx fields.

I tried to add 2 lines just before the save(), like this:

$data->set('email', 'test@gmail.com'); // DOES NOT WORK, DATA IS NOT SAVED
$data->set('my_extended_field', 'test'); // WORKS FINE, DATA IS SAVED

And again, the email field is not saved into the database…

You have now 3 different objects that contain your user-data:

  • $data (class “userData” -> your custom data)
  • $data->User (class “modUser”)
  • $data->Profile (class “modUserProfile”)

‘email’ is a field of the class “modUserProfile”, so you have to set it there.

$data->Profile->set('email', 'test@gmail.com');

Great ! It works perfectly now ! Thank you !

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.