silverstripe-cms icon indicating copy to clipboard operation
silverstripe-cms copied to clipboard

Archiving a DataObject calls the validator

Open TomOudeRengerink opened this issue 3 years ago • 4 comments

Affected Version

4.11.4

Description

The getCMSValidator method of a DataObject does not respect the doArchive action.

Steps to Reproduce

  1. Add the getCMSValidator method to a DataObject.
public function getCMSValidator(): RequiredFields
{
    return new RequiredFields([
        'Title',
    ]);
}
  1. Archive a DataObject (record) where the required field is empty.

  2. This will validate the DataObject which is not desirable.

TomOudeRengerink avatar Aug 04 '22 13:08 TomOudeRengerink

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.

michalkleiner avatar Aug 04 '22 15:08 michalkleiner

There’s a method in Form called setValidationExemptActions() (or something similar) - do we just need to add the archive action to that?

lozcalver avatar Aug 05 '22 11:08 lozcalver

There’s a method in Form called setValidationExemptActions() (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.

TomOudeRengerink avatar Aug 05 '22 14:08 TomOudeRengerink

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.

GuySartorelli avatar Aug 08 '22 03:08 GuySartorelli