Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archiving a DataObject calls the validator #2760

Open
TomOudeRengerink opened this issue Aug 4, 2022 · 4 comments
Open

Archiving a DataObject calls the validator #2760

TomOudeRengerink opened this issue Aug 4, 2022 · 4 comments

Comments

@TomOudeRengerink
Copy link

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.

@michalkleiner
Copy link
Contributor

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.

@kinglozzer
Copy link
Member

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

@TomOudeRengerink
Copy link
Author

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.

@GuySartorelli
Copy link
Member

$form->setValidationExemptActions([
'restore',
'revert',
'deletefromlive',
'delete',
'unpublish',
'rollback',
'doRollback',
'archive',
]);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants