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

Validate before opening draft preview #106

Open
cannorin opened this issue Sep 7, 2023 · 3 comments
Open

Validate before opening draft preview #106

cannorin opened this issue Sep 7, 2023 · 3 comments
Labels
request Request for a feature

Comments

@cannorin
Copy link

cannorin commented Sep 7, 2023

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 mattmilburn added the request Request for a feature label Sep 28, 2023
@mattmilburn mattmilburn pinned this issue Sep 28, 2023
@mattmilburn
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

@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 👍🏻

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

No branches or pull requests

2 participants