OnBeforeDocFormSave
Last updated Oct 14th, 2026 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn moreEvent: OnBeforeDocFormSave¶
Fires in the Manager before the Resource create or update processor persists the Resource. Use it to change fields on the $resource object, or to block the save.
- Service: 1 - Parser Service Events
- Group: Resources
Fired from MODX\Revolution\Processors\Resource\Create and Update (also UpdateFromGrid). Flow in those processors:
- Fields are applied to the Resource object
OnBeforeDocFormSaveruns- The processor calls
$resource->save() - After-save work runs (TVs on update, resource groups, and so on)
OnDocFormSaveruns
Changing fields vs calling save()¶
For Resource fields, call $resource->set(...). The processor saves after your plugin returns, so the plugin needs no $resource->save().
An early $resource->save() is the exception: it writes the row before Template Variables and other after-save steps run, and it blurs create-versus-update timing. For TV changes use OnDocFormSave with setTVValue.
Blocking the save¶
Plugins should return nothing (or an empty value) on success. A non-empty return value is treated as an error message and is logged. Depending on how it is returned, the save may still continue.
To stop the save and show a message in the Manager, pass text to $modx->event->output(...). That text must be plain text (no HTML), or the modal can hang.
Event Parameters¶
| Name | Description |
|---|---|
| mode | new or upd (modSystemEvent::MODE_NEW / MODE_UPD) |
| resource | Reference to the modResource object (passed by reference) |
| id | Resource ID. 0 for new Resources that are not saved yet |
| reloadOnly | Create path only: true when the processor just reloads the form after a validation error instead of saving |
| data | Update path only: the current fields of the Resource as an array ($resource->toArray()) |
| object | Update path only: alias of resource — a reference to the same object |
Examples¶
Require a field¶
if (empty($resource->get('longtitle'))) {
$modx->event->output('Long title is required!');
return;
}
Set a field before the processor saves¶
if ((int) $resource->get('parent') === 123) {
$resource->set('template', 4);
}
Block creation or updates without introtext¶
<?php
switch ($modx->event->name) {
case 'OnBeforeDocFormSave':
if ($mode == modSystemEvent::MODE_UPD) {
if (!$resource->get('introtext')) {
$modx->event->output("Please fill in the Intro Text field.");
}
} elseif ($mode == modSystemEvent::MODE_NEW) {
$modx->event->output("You cannot create resources!");
}
break;
}
See Also¶
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn more










