silverstripe-cms
silverstripe-cms copied to clipboard
Archiving a DataObject calls the validator
Affected Version
4.11.4
Description
The getCMSValidator method of a DataObject does not respect the doArchive action.
Steps to Reproduce
- Add the
getCMSValidatormethod to aDataObject.
public function getCMSValidator(): RequiredFields
{
return new RequiredFields([
'Title',
]);
}
-
Archive a
DataObject(record) where the required field is empty. -
This will validate the
DataObjectwhich is not desirable.
Yeah, that's been like that for some time now and we had users complaining about it as well. Since our validation was usually simple we asked them to populate those required fields and then archive it.
There’s a method in Form called setValidationExemptActions() (or something similar) - do we just need to add the archive action to that?
There’s a method in
FormcalledsetValidationExemptActions()(or something similar) - do we just need to add the archive action to that?
Already tried that... seems that when the archive action kicks in the validationExemptActions array is empty.
https://github.com/silverstripe/silverstripe-cms/blob/de8d7632c23e3d18b862d5cb6187dba35102ca8e/code/Controllers/CMSMain.php#L1379-L1388
CMSMain does this for SiteTree (or, more specifically, does it for the edit form most typically used for SiteTree), which is why this doesn't happen for pages.
DataObject subclasses are most typically edited using a gridfield, which means the form that needs this treatment is the one produced by GridFieldDetailForm_ItemRequest::ItemEditForm().
You should be able to work around this by adding the following extension to GridFieldDetailForm_ItemRequest:
class GridFieldValidationExcemptExtension extends Extension
{
public function updateItemEditForm(Form $form): void
{
$form->setValidationExemptActions([
'doArchive',
// You probably want to also add other actions like delete, restore, etc
]);
}
}
Ideally this will be done in core directly in GridFieldDetailForm_ItemRequest::ItemEditForm() through a PR. If someone wants to do that I'd be happy to look at it.