首页 > 解决方案 > 如何将错误消息放在 Prestashop 1.7 内的表单框中?

问题描述

我正在为前台创建一个recaptcha 模块,所有代码都可以,但是错误消息会发错地方。

我使用的是 Prestashop 1.7.6.1,错误消息打印在前台顶部,我需要在表单框中打印错误消息。

我设置了这样的错误消息:modules/recaptcha/recaptcha.php

public function hookActionRecaptchaAccountValidation()
{
    if (!$this->isValidateRecaptcha()) { // Logic to validate recaptcha
        // Set my error message
        $context->controller->errors[] = $this->l('It was not possible to validate the reCaptcha.');
    }
}

我正在重写 AuthController 来执行我的自定义钩子,这将使 recaptcha 验证逻辑,这里没关系,我也放了这段代码,以防你需要。

class AuthController extends AuthControllerCore
{
    public function initContent()
    {
        if(Tools::isSubmit('submitLogin')){
            // Execute my custom hook
            Hook::exec('actionRecaptchaAccountValidation');

            if(sizeof($this->context->controller->errors)){ // Verify if has error message
                $login_form = $this->makeLoginForm()->fillWith(
                    Tools::getAllValues()
                );    
                $this->context->smarty->assign([
                    'login_form' => $login_form->getProxy(),
                ]);
                $this->setTemplate('customer/authentication');
                FrontController::initContent();
                return;
            }
        }

        parent::initContent();
    }
}

...那么,我怎样才能像这个例子一样将错误消息放在表单框中?

请检查这张图片,我在那里准确地展示了我需要的东西。 https://i.imgur.com/Syzrf2C.png

标签: phpprestashopprestashop-1.7prestashop-modules

解决方案


我认为你应该更好地研究CustomerLoginForm类并考虑覆盖它。该submit方法可能正是您正在寻找的。只需再添加一个验证并通过$this->errors[''][]构造定义您的错误


推荐阅读