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) {
// ...HERE I DON'T KNOW HOW TO BLOCK THE NEXT LINE IF THE VALIDATION FAILS...
$data->save();
}
...
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) {
$data->save();
} 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:
[[+value_error]]
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.
I think the validation snippet is ok and the problem is just the combination of the snippets ExtUserUpdateProfile and UpdateProfile
[[!ExtUserUpdateProfile]]
[[!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)
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
$data->save();
And again, the email field is not saved into the database…