Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP模板引擎上显示错误_Php_Templates_Error Handling - Fatal编程技术网

如何在PHP模板引擎上显示错误

如何在PHP模板引擎上显示错误,php,templates,error-handling,Php,Templates,Error Handling,我正在为我的一个项目做准备 模型中的一个方法检查新用户提供的电子邮件地址,并告知他们试图使用的电子邮件是否存在 差不多 class UserModel extends BaseModel { public $errors = []; public function validate() { if (filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL) === fals

我正在为我的一个项目做准备

模型中的一个方法检查新用户提供的电子邮件地址,并告知他们试图使用的电子邮件是否存在

差不多

class UserModel extends BaseModel
{
    public $errors = [];

    public function validate()
    {
        if (filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL) === false) {
            $this->errors[] = 'Invalid email';
        }
        if ($this->emailExists($this->request->post['email'])) {
            $this->errors[] = 'Email already exist';
        }
    }

    protected function emailExists($email)
    {
        $sql = 'SELECT * FROM user_account WHERE email = :email';
        -----
        -----
        $stmt->execute();
        return $stmt->fetch() !== false;
    }
}
在控制器中

public function register()
{
    $this->load->getModel('UserModel');

    if ($this->model_UserModel->registerUser($this->request->post)) {
        echo "Success ... load (redirect) second page";
    } else {
        $data ['error'] = $this->model_UserModel->errors;
        echo $this->template->render('home/home', $data);
    }
}
如果电子邮件存在,并且I var dump$data['error'],它会按照UserModel中的validate方法中的定义显示“电子邮件已经存在”

现在,我试图通过在tpl文件中添加这些行来获取我的主模板上的错误消息

<?php if (!empty($this->e($errors))): ?>
    <?php foreach($errors as $error): ?>
        <li><?=$this->e($error)?></li>
    <?php endforeach ?>
<?php endif;?>

  • 但是现在如果我点击注册页面,模板上会显示

    注意:未定义变量:第14行C:\xampp\htdocs\vem\App\Views\template\register\registeruser.tpl中的错误

    我该如何前进


    我甚至尝试设置
    $this->e($error)='
    ,但naa显示另一个错误。

    在控制器上,您正在设置变量
    error
    ,但在模板中,您正在访问变量
    errors
    (使用
    s
    )。 试一试


    您需要使用
    错误
    而不是
    错误

    <?php if (!empty($this->e($error))): ?>
        <?php foreach($error as $error_item): ?>
            <li><?=$this->e($error_item)?></li>
        <?php endforeach ?>
    <?php endif;?>
    
    
    

  • $errors
    未定义。这就是错误消息所说的。您可能需要将
    $data['error']
    更改为
    $data['errors']
    。在我看来,这是个“打字错误问题”,很快就要结束了。
    <?php if (!empty($this->e($error))): ?>
        <?php foreach($error as $error_item): ?>
            <li><?=$this->e($error_item)?></li>
        <?php endforeach ?>
    <?php endif;?>