CodeIgniter 4 错误验证未读取

CodeIgniter 4 Error Validation Not Reading

我正在使用 Codeigniter 4 和 SQL 数据库制作联系表。表单将通过按钮提交,因此无论何时单击它,它都在验证,如果所有字段都已填写,则显示消息并将信息保存到数据库没有问题,但是当字段为空时,它不显示错误消息,并继续声明该文件不存在。

好吧,我现在被错误消息困住了。我不确定代码有什么问题。我错过了什么吗?

有人可以帮我解决这个问题吗?我很感激我能得到的所有帮助。

下面是我的代码:

App/config/Routes.php

$routes->get('contact', 'Contact::contact');
$routes->post('contact/save', 'Contact::save');

App/Controller/Contact.php

 <?php

namespace App\Controllers;

use App\Models\ContactModel;

class Contact extends BaseController
{

    public function __construct()
    {
        helper(['url', 'form']);
    }

    //CONTACT PAGE
public function contact()
{

    $data = [
        'meta_title' => 'Contact | MFD',
    ];

    return view('page_templates/contact', $data);
}

//SAVE
public function save()
{

    $validation = $this->validate([
        'name' => [
            'rules' => 'required',
            'errors' => [
                'required' => 'Your full name is required'
            ]
        ],
        'email' => [
            'rules' => 'required|valid_email|is_unique[users.email]',
            'errors' => [
                'required' => 'Email is required',
                'valid_email' => 'You must enter a valid email',
            ]
        ],
        'title' => [
            'rules' => 'required',
            'errors' => [
                'required' => 'Title is required',
            ]
        ],
        'content' => [
            'rules' => 'required',
            'errors' => [
                'required' => 'Content is required',
            ]
        ],
    ]);


    if (!$validation) {
        return view('contact', ['validation' => $this->validator]);
    } else {

        // Let's Register user into db
        $name = $this->request->getPost('name');
        $email = $this->request->getPost('email');
        $title = $this->request->getPost('title');
        $content = $this->request->getPost('content');

        $values = [
            'name' => $name,
            'email' => $email,
            'title' => $title,
            'content' => $content,
        ];

        $contactModel = new ContactModel();
        $query = $contactModel->insert($values);
        if ($query) {
            return redirect()->to('contact')->with('success', 'Your message are successful sent');
        } else {
            return redirect()->to('contact')->with('fail', 'Something went wrong');
        }
    }
}

}

App/Views/page_templates/contact.php

<form action="<?= base_url('contact/save'); ?>" method="post" role="form" class="php-email-form">
<?= csrf_field(); ?>

<?php if (!empty(session()->getFlashdata('error'))) : ?>
    <div class="alert alert-danger"><?= session()->getFlashdata('error'); ?></div>
<?php endif ?>

<?php if (!empty(session()->getFlashdata('success'))) : ?>
    <div class="alert alert-success"><?= session()->getFlashdata('success'); ?></div>
<?php endif ?>

<div class="row">
    <div class="col-md-6 form-group">
        <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" value="<?= set_value('name'); ?>">
        <span class="text-danger"><?= isset($validation) ? display_error($validation, 'name') : '' ?></span>
    </div>
    <div class="col-md-6 form-group mt-3 mt-md-0">
        <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" value="<?= set_value('email'); ?>">
        <span class="text-danger"><?= isset($validation) ? display_error($validation, 'email') : '' ?></span>
    </div>
</div>
<div class="form-group mt-3">
    <input type="text" class="form-control" name="title" id="title" placeholder="Title">
    <span class="text-danger"><?= isset($validation) ? display_error($validation, 'title') : '' ?></span>
</div>
<div class="form-group mt-3">
    <textarea class="form-control" name="content" rows="5" wrap="hard" placeholder="Message"></textarea>
    <span class="text-danger"><?= isset($validation) ? display_error($validation, 'content') : '' ?></span>
</div>

<div style="height: 10px;"></div>
<div class="text-left button">
    <button type="submit" name="submit">Send Message</button>
</div>
<div style="height: 10px;"></div>

</form>

好的,我已经解决了我的代码的问题。这真的是一个小错误,或者我可以说是粗心的错误哈哈现在感觉很愚蠢,但我需要改变并让它发挥作用。

这部分是:return view('contact', ['validation' => $this->validator]); 我没有像那样做,而是错过了放置文件以正确调用视图页面的代码,所以我只添加了我的文件夹名称

page_templates

如果我将它包含在代码中,它应该如下所示:-

return view('page_template/contact', ['validation' => $this->validator]);

然后就可以了哈哈