Jump to content

Tools::displayError not showing


banditbirds

Recommended Posts

Hi guys,

 

I'm trying display an error message if the customer is not logged in.

if ($this->context->customer->isLogged())
{
  ..........        
} 
else
{
    $this->errors[] = Tools::displayError('You must be logged in to request a custom order');
} 

But the error message isn't being displayed...

 

Anyone have an idea what I'm missing?

 

The file is in: /modules/blockquote/controllers/front/display.php

 

Thanks in advance!!

 

Simon

 

Link to comment
Share on other sites

Hi, thanks for the replies.

 

Yes in the initContent:

  public function initContent()
  {
    $varRequestDetails = '';
    $varRequestPrice = '';
    $sql = ''; 
    $user_id ='';
    $errors = array();
  
    parent::initContent();
    $this->setTemplate('display.tpl');
  }

Not sure what you mean flobrflo??

 

Cheers!

 

Simon

Link to comment
Share on other sites

I don't use blockquote,

 

but if you assign to :

$this->errors[] = Tools::displayError('You must be logged in to request a custom order');

$this->errors contain an array of string, Tools::displayError() only translate the message in the current language

(in all the error string, if it wasn't found, he let the string like you write it)

 

so in your module you need to have something wich display the error.

(maybe in your display.tpl)

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

I managed to skip giving the message. I wanted a popup message and the user to be redirected to log in to their account.

 

I managed to take this from blockwishlist.

 

in my front controller:

$errors = array();
Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('blockquote', 'display')));

$this->context->smarty->assign(array(
'id_customer' => (int)$this->context->customer->id,
'errors' => $errors,
'form_link' => $errors,
));

$this->setTemplate('display.tpl');
Link to comment
Share on other sites

The code I pasted above redirects the user to log in. And then after successful log in, takes the user back to my module's page (display.tpl).

 

I'm stolen another line from blockwishlist, as I want to do some validation on the data entry and display an error when a field is left blank. So similar to my original problem, I can' get the error to display. Here is the line from the controller:

 

if (empty($RequestDetails))
$errors[] = $this->module->l('You must specify some request details.', 'display');

So do you think I need to display this error somehow in the display.tpl??

 

Thanks!

Link to comment
Share on other sites

If you have to redirect, use context to keep the error.

something like this before redirect :

$this->context->Custom_error = $this->errors;

after you're supposed able to get error with:

p($this->context->Custom_error);
Link to comment
Share on other sites

Thanks for the replies!

 

@ loulou66

 

I tried that, but nothing is happening when I click my Submit button. It just goes back to the same page.

 

<div id="main_body" >
   <div id="form_container">
   {include file="$tpl_dir./errors.tpl"} 
        <form action="{$link->getModuleLink('blockquote', 'display')|escape:'html'}" method="post" class="appnitro" id="form_840994">  

.......

@flobrflo

 

Sorry, where do I need to put these:

 

if (empty($RequestDetails))
   $errors[] = $this->module->l('You must specify some request details.', 'display');
   $this->context->Custom_error = $this->errors;
if (!count($errors))
{
    ....

Thanks!

 

Simon

 
Link to comment
Share on other sites

mmhh..

if i understand your problem:

 

1)In your module:

//first step :

$this->context->custom_error = $this->module->l('You must specify some request details.', 'display');
Tools::Redirect ...

2) In your Front:

// sec. step

$custom_error = $this->context->custom_error;
/*
you can display it with: p($cutom_error)
or 
echo '<div class="alert alert-danger">'.$custom_error.'</div>';
or anyelse you want
*/
Link to comment
Share on other sites

Thanks flobrofl. Luckily I don't need to display an error before my redirect, as it redirects the user to the authentication page, which is obvious what the customer needs to do.

 

The problem I am having now, is when validating the data on submitting the form, its not displaying the error message from $tpl_dir ./errors.tpl.

 

I'm not redirecting this part.

 

if (Tools::isSubmit('submit')) //submit form
{
  $errors = array();

  //get some data

  $varRequestDetails = Tools::getValue('formRequestDetails'); //get value from form
 
  if (empty($varRequestDetails)) //if the value is empty
  {
      $errors[] = $this->module->l('You must specify some request details.', 'display'); //display error
  }
  else
  //do stuff

Thanks,

 

Simon

Link to comment
Share on other sites

Oh okay, have you got any error?

 

if not, do some verification:

 

1) Check if you assign to smarty correctly your errors:

$this->context->smarty->assign(array(
'errors' => $errors,
);

//like you write it few post ago ^^

2) Check in your tpl if you correctly use the $errors : it's an array and smarty can't display an array

edit: so you have to encode it =)

{$errors|@json_encode}

3) Check in php if the message is correctly added to errors:

//the last chance ^^
$errors[] = $this->module->l('You must specify some request details.', 'display'); //display error
d($errors);
Edited by flobrflo (see edit history)
Link to comment
Share on other sites

Sorry flobrflo, I should have posted the full code. Where I am trying to show an error, I'm not setting the smarty array, that is further on for another error.

 

Here is my full code. I will try the other two suggestions you made.

 

Thanks for the good advice!

 

public function postProcess()
{
      if (Tools::isSubmit('submit'))
      {
        $errors = array();
        if ($this->context->customer->isLogged())
        {
          $varUserId=(int)$this->context->customer->id;
             
          $varRequestDetails = Tools::getValue('formRequestDetails');
          $varDate = Tools::getValue('formDate');
          $varRequestPrice = Tools::getValue('formRequestPrice');
          if (empty($varRequestDetails))
          {
              $errors[] = $this->module->l('You must specify some request details.', 'display'); // The error that's not showing!
          }
          else
          {
             $sql = "INSERT INTO `ps_custom_order` (`request_details`,`date_needed`,`request_price`,`user_id`)
                    VALUES('{$varRequestDetails}','{$varDate}','{$varRequestPrice}','{$varUserId}');";

             if (!Db::getInstance()->execute($sql))
             {
               die('Error etc.');
             }
             else
             {
                Tools::redirect('index.php');
             }
          }
       } 
       else
       {
         Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('blockquote', 'display')));


      $this->context->smarty->assign(array(
      'id_customer' => (int)$this->context->customer->id,
      'errors' => $errors,
      'form_link' => $errors,
      ));


      $this->setTemplate('display.tpl');
       } 
                 
      }
  }
Edited by banditbirds (see edit history)
Link to comment
Share on other sites

okay.

 

So, there is no error =( (with $errors)

$errors[] = $this->module->l('You must specify some request details.', 'display'); 
// the traduction 'You must specify some request details' is in the translation category display? 
//yes => translate the message
//no => keep the original

you probably shoul add "}" here :

else
{
    Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('blockquote', 'display')));
//}

if you don't, the module will redirect to the next page and don't call your template display

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

Thanks!

 

I'm not sure about the 'translation category display'??

 

The 'else' block is all one set of actions, that section seems to be running fine. But I moved the redirect to the end.

 

else
{
      $this->context->smarty->assign(array(
              'id_customer' => (int)$this->context->customer->id,
              'errors' => $errors,
              'form_link' => $errors,
      ));

      $this->setTemplate('display.tpl');
      Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('blockquote','display')));
 } 
Link to comment
Share on other sites

i mean :

$errors[] = $this->module->l('You must specify some request details.', 'display'); 

presta will search the translation for your message in a "display" liste for your current language.

 

like if you write into smarty:

{l="You must specify some request details." mod="display"}
Link to comment
Share on other sites

Ok...I've been looking at blockwishlist module which is using:

$errors[] = $module->l('Invalid token', 'mywishlist');

I could copy what they have done for my module. Where is this 'mywishlist' list?

 

I thought it referred to: /modules/blockwishlist/views/templates/front/mywishlist.tpl

 

Which is why I used 'display' in my module, to refer to: /modules/blockquote/views/templates/front/display.tpl

 

 

I don't have any translations in my /modules/blockquote/translations/

Link to comment
Share on other sites

$errors[] = $module->l('Invalid token', 'mywishlist');
//referer to fr.php or en.php (or any other language by default) in blockwishlist module

It's not a problem if you don't have translation in your module.

Prestashop just let the message like it was write.

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

In the display.tpl?

 

I don't know how to do this. blockwishlist module has the following in the .tpl

<input type="text" id="name" name="name" class="inputTxt" value="{if isset($smarty.post.name) and $errors|@count > 0}{$smarty.post.name|escape:'html':'UTF-8'}{/if}" />
Link to comment
Share on other sites

  • 2 years later...

Ok, I've got that div in my tpl:

 

         <div class="alert alert-danger"></div>
         {foreach $errors as $item}<li>{$item}<li>
         {/foreach}
 
And I've got the form failing here (I know because the echo is working):
 
        echo "File is not an image.";
        $errors[] = $this->module->l('File is not an image.', 'customform');
 
But the errors aren't being saved to the $errors array. Screenshots attached.
 
 

Any idea what I'm doing wrong with the the array in the tpl?

post-770181-0-48943400-1472737722_thumb.png

Link to comment
Share on other sites

  • 8 months later...

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...