banditbirds Posted May 22, 2014 Share Posted May 22, 2014 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 More sharing options...
flobrflo Posted May 22, 2014 Share Posted May 22, 2014 Where and how did you retunr $this->error? Link to comment Share on other sites More sharing options...
CartExpert.net Posted May 22, 2014 Share Posted May 22, 2014 Hi. Are you assigning the error array to the template? Regards.Robin.The CartExpert Team Link to comment Share on other sites More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 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 More sharing options...
flobrflo Posted May 22, 2014 Share Posted May 22, 2014 (edited) 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 May 22, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 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 More sharing options...
flobrflo Posted May 22, 2014 Share Posted May 22, 2014 You got all of this in the same function? Link to comment Share on other sites More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 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 More sharing options...
loulou66 Posted May 22, 2014 Share Posted May 22, 2014 Hi in your display.tpl you must include error.tpl file {include file="$tpl_dir./errors.tpl"} @++ loulou66 Link to comment Share on other sites More sharing options...
flobrflo Posted May 22, 2014 Share Posted May 22, 2014 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 More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 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 More sharing options...
flobrflo Posted May 22, 2014 Share Posted May 22, 2014 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 More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 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 More sharing options...
flobrflo Posted May 22, 2014 Share Posted May 22, 2014 (edited) 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 May 22, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 (edited) 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 May 22, 2014 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted May 22, 2014 Author Share Posted May 22, 2014 This is what I get when I try: d($errors) Array ( [0] => You must specify some request details. ) END 1 Link to comment Share on other sites More sharing options...
flobrflo Posted May 23, 2014 Share Posted May 23, 2014 (edited) 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 May 23, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted May 23, 2014 Author Share Posted May 23, 2014 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 More sharing options...
flobrflo Posted May 23, 2014 Share Posted May 23, 2014 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 More sharing options...
banditbirds Posted May 23, 2014 Author Share Posted May 23, 2014 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 More sharing options...
flobrflo Posted May 23, 2014 Share Posted May 23, 2014 (edited) $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 May 23, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted May 23, 2014 Author Share Posted May 23, 2014 (edited) I have a /translations folder and an en.php. But it's empty. Ok...I don't understand why the error isn't displaying Does anyone else have any ideas?? Edited May 23, 2014 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
flobrflo Posted May 23, 2014 Share Posted May 23, 2014 (edited) ?? your variable errors is correct... you have to correctly display $errors in the tpl... Edit : check smarty documentation Edited May 23, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted May 23, 2014 Author Share Posted May 23, 2014 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 More sharing options...
flobrflo Posted May 23, 2014 Share Posted May 23, 2014 something like : {$errors.0} Link to comment Share on other sites More sharing options...
banditbirds Posted May 23, 2014 Author Share Posted May 23, 2014 I wanted it to display a popup error message, or something similar... I was hoping prestashop would have something for this, or do I need a javascript or something to do this? Thanks for all your help! Link to comment Share on other sites More sharing options...
flobrflo Posted May 23, 2014 Share Posted May 23, 2014 you got the alert class: <div class="alert alert-danger"></div> // alert-info // alert-warning in js, presta may got a function but i don't know it Link to comment Share on other sites More sharing options...
banditbirds Posted May 27, 2014 Author Share Posted May 27, 2014 Thanks! I'll try it out. Link to comment Share on other sites More sharing options...
banditbirds Posted September 1, 2016 Author Share Posted September 1, 2016 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? Link to comment Share on other sites More sharing options...
banditbirds Posted September 1, 2016 Author Share Posted September 1, 2016 Solved that! Had to add the array in a public variable: public $errors = array(); Link to comment Share on other sites More sharing options...
aflores Posted May 29, 2017 Share Posted May 29, 2017 This worked form me, for a carrier module: $this->context->controller->errors[] = $this->l('Custom Error'); 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