REGE Posted February 5 Share Posted February 5 I have a module that adds an additional field to a product form. I have to check before saving whether it is completed. I do this with hookActionObjectProductUpdateBefore like this: public function hookActionObjectProductUpdateBefore($params): bool { $product = Tools::getValue('product'); $test_field = $product['description']['test_field']; // check if the variable exists and has a value if (empty($test_field)) throw new PrestaShopException('Missing test field'); return true; } In case of an error, the above method returns the following message: Quote An unexpected error occurred. [PrestaShop\PrestaShop\Core\Exception\CoreException kod 0]: Missing test field Throwing a PrestaShopException is fine for reporting system errors, but not ideal for presenting short error messages to the end user, especially in the context of input forms. Is it possible to pass the error to the product form in a more elegant form? Link to comment Share on other sites More sharing options...
Nickz Posted February 5 Share Posted February 5 1 hour ago, REGE said: Is it possible to pass the error to the product form in a more elegant form? it sure is. Those error messages are formed, so you need to search the entire shop plus the Database for it and mod the sentence. Make back ups before touching anything. Link to comment Share on other sites More sharing options...
REGE Posted February 13 Author Share Posted February 13 Thank you for your answer. Currently I've modified them using JS. When I have more time I will dig into the code. Link to comment Share on other sites More sharing options...
Roxayl Posted March 14 Share Posted March 14 (edited) If you wish to display errors next to the input (at least on the V1 product page), you could try to display your error message the same way other the default form field validation errors are displayed: public function hookActionObjectProductUpdateBefore(array $params): void { /** @var \Product $product */ $product = $params['object']; if ($notValidated) { $field = 'step1_reference'; // see the input name/id. $message = 'Your error message, whatever.'; // Clean any output buffering done through the Symfony controller. ob_end_clean(); // The product page JS code triggers an error message if a 400 HTTP code is returned. header('HTTP/1.1 400 Bad Request'); // Display errors. You could display validation errors for multiple fields too. Apparently, no need to explicitly define the Content-Type. echo json_encode([$field => [$message]); exit; } } Which creates the following output: There's probably a more elegant way do to that, idk. You should probably check that you are in the correct context before displaying your errors like that (e.g. AJAX validation through ProductController, etc etc). Edited March 14 by Roxayl (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now