How can I show a custom popup message in the admin ui while creating an entry if a certain condition met inside beforeCreate hook?

System Information
  • Strapi Version: 3.6.9
  • Operating System: Windows 10
  • Database: MySQL
  • Node Version: 14.20.0
  • NPM Version: 6.14.17
  • Yarn Version: 1.22.19

Hi Everyone
I have a Strapi project. I have created a collection to store product data and prices. Now I want to show a custom message if the price is greater than one million. I want to show it like a warning message, not an error message. I found on Strapi docs that I can show error messages using throw strapi.errors.badRequest('Price must be between 1.000.000 and 150.000.000');
the problem is it will stop the hook from saving my data and I don’t want that. I just want to show a custom warning message just to let the user know that it doesn’t meet this requirement.
Is there any way to do this?

1 Like

It’s been a while since the question is asked but still it might help someone else.

First requirement here is that your backend should allow to save the value without constrains.
On the frontend side you can do your validation before saving, and then use showNotification() hook from strapi to send a warning message as a parallel process in your saveHandler.

import { useNotification } from "@strapi/helper-plugin";

...

const YourAwesomeComponent = () => {
  const showNotification  = useNotification();

  const  save = () => {
    if (price > 150000000 || price <1000000 ) {
      // This shows a notification message as a warning
      // @see https://github.com/strapi/strapi/blob/v4.21.1/packages/core/helper-plugin/src/features/Notifications.tsx#L26
      // If you are using newer version of strapi then check the new version of this file, it may be changed over time
      showNotification({ message: "Price must be between 1.000.000 and 150.000.000", title: "Success", type: 'softWarning'});
    }

    // proceed to save logic
  }

  // ...
}

The same way you can use to display success message as well, just by changing the type of showNotification