Jump to content

Display an error message from a module in product admin


Recommended Posts

Hi,

 

I have a module in the admin product form (hookDisplayAdminProductsExtra) with a form that should alway be complete by the user, when he add a new product.

 

I would like to display the error message when a field is empty or not valid, exactly the same way the fields of the product form does. I've been looking in this file "\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php" but I'm not sur how I can do...

 

I have a Prestashop 1.7.1.1

 

Thanks.

Edited by spinelle (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...
  • 8 months later...

I found a solution.

First, In your hook you must set 

header("HTTP/1.0 400 Bad Request");

After this, the error message will appear at the top-right side of the screen (predefined error message of the Prestashop).

You can show your custom error message only if you have some tpl file in your module. This file must contain an element, to which the error message will be attached. It can be both input or any other HTML element.

In my example I have table. You should give it an id that must starts with "form_". E.g.:

<table class="table" id="form_some_id">

Then, in your PHP file (after the header been set) you should return an error in this format:

die(json_encode(array('some_id' => array($this->l('Error text.')))));

Note, that structure must be as in example. Key of the array should be as ID of the element, but without "form_".
The text of an error will be shown after the element.

Hope this will help somebody.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...
On 4/28/2017 at 5:01 PM, spinelle said:

Thank you for your answer.

 

Yes I did try that. I also tried: $this->context->controller->errors[] = $this->l('My error');

But none of them worked.

This worked for me. I code a module for a client of mine (1.6.0.x version) and this solved my hands. Thanx

Link to comment
Share on other sites

  • 3 years later...

I have some errors in the error log files under public_html > error_log file. Can someone have any idea on how to fix this?

[04-Jul-2023 10:29:02 Europe/Bucharest] PHP Deprecated:  Invalid characters passed for attempted conversion, these have been ignored in /home/myshop/public_html/classes/Tools.php on line 2408
[04-Jul-2023 10:29:02 Europe/Bucharest] PHP Deprecated:  Invalid characters passed for attempted conversion, these have been ignored in /home/myshop/public_html/classes/Tools.php on line 2409

and these errors from php-ps-info-1.1 > error_log

[04-Jul-2023 03:27:47 UTC] PHP Notice:  Trying to access array offset on value of type int in /home/myshop/public_html/php-ps-info-1.1/phppsinfo.php on line 364

the lines of code the error_logs are pointing too are these (see the pictures attached):

PrestaShop 1.7.8.9, PHP 7.4.3, Wareehouse theme (latest version)

Thank you!

liniile-de-cod-din-error-log.png

phppsinfo_error_log.png

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...