mattmilburn / strapi-plugin-preview-button

A plugin for Strapi CMS that adds a preview button and live view button to the content manager edit view.
MIT License
100 stars 35 forks source link

Validate before opening draft preview #106

Open cannorin opened 1 year ago

cannorin commented 1 year ago

Strapi allows draft entries not to have all the required fields, which is great since users can save an unfinished entry as a draft and finish it later.

However with this plugin, users can try to open a preview of a draft entry, which may lack the fields needed to display the preview page correctly (e.g. the content of the page). I can workaround this by adding error messages to my frontend app, but it should be more intuitive for users to fail early and display validation errors on Strapi.

This may be difficult to implement however, as I can find someone on the official forum who are struggling to trigger validation from their plugin: https://forum.strapi.io/t/trigger-validation/17103.

mattmilburn commented 11 months ago

Hi @cannorin That would be a nice feature to include 👍🏻 I don't think I knew that Strapi allowed empty required fields for drafts.

I have to agree that custom fields are currently lacking a proper method for frontend validation, but with this being a custom plugin it won't be limited in that way. It's also important to note that plugin configurations can include callbacks, but those callbacks will only be usable on the server - they are not passed to the frontend at all.

After looking at this compileWithHooks function in the plugin, it might just need an additional hook to handle validation when clicking the preview button (same with the copy button). I'm thinking the custom validation hook would return an object like below which would prevent the button from opening the preview link and instead, display a toast error. This is just off the top of my head.

{
  result: false,
  error: 'Here is a custom error message for the toast UI in Strapi.',
}

Does this sound like it would be helpful for your situation or did I miss anything?

cannorin commented 11 months ago

I was wondering how can I possibly trigger the built-in validation feature (the one that triggers when you try to publish your entry). If that is not possible, yes, custom validation hooks would be the best thing we can have right now.

mattmilburn commented 11 months ago

@cannorin Hmm if I'm not mistaken, when you attempt to publish an entry Strapi will POST the data where there is a server-side validation. It usually happens so fast that it's easy to mistake it for happening on the frontend and honestly, things should always be validated in both the client and the server anyway. But currently there are not a lot of options for developers to utilize everything built-in with Strapi so I think a custom validation hook might be necessary here.

This sounds like a feature worth implementing if I did not already mention that 👍🏻