错误验证不出现 CodeIgniter 4

huangapple go评论72阅读模式
英文:

Error validation does not appear CodeIgniter 4

问题

我在CodeIgniter 4的验证错误方面遇到了问题,当我点击按钮时,错误消息根本不会出现,有什么问题吗?请帮忙,我非常感激。

以下是代码:

控制器

public function Daftar()
{
    // 如果验证通过
    if ($this->validate([
        'nim' => [
            'label' => 'NIM',
            'rules' => 'required|is_unique[tbl_anggota.nim]',
            'errors' => [
                'required' => '{field} Masih Kosong!',
                'is_unique' => '{field} NIM Sudah Terdaftar!',
            ]
        ],
        'nama' => [
            'label' => 'Nama',
            'rules' => 'required',
            'errors' => [
                'required' => '{field} Masih Kosong!',
            ]
        ],
        'password' => [
            'label' => 'Password',
            'rules' => 'required',
            'errors' => [
                'required' => '{field} Masih Kosong!',
            ]
        ],
        'ulangi_password' => [
            'label' => 'Ulangi Password',
            'rules' => 'required|matches
		
输入密码查看隐藏内容

'
,
'errors' => [ 'required' => '{field} Masih Kosong!', 'matches' => '{field} Password Tidak Sama!', ] ], ])) { } else { // 如果验证未通过 session()->getFlashdata('errors', \Config\Services::validation()->getErrors()); return redirect()->to(base_url('Auth/Login'))->withInput('validation', \Config\Services::validation()); } }

视图

<?php
// 错误通知
$errors = session()->getFlashdata('errors');
if (!empty($errors)) { ?>
    <div class="alert alert-danger" role="alert">
        <h4>Periksa Entry Form</h4>
        <ul>
            <?php foreach ($errors as $key => $error) { ?>
                <li><?= esc($error) ?></li>
            <?php } ?>
        </ul>
    </div>
<?php } ?>

非常感谢!!

我尝试查找解决方法,但仍然不起作用。

英文:

I have a problem with the validation error for CodeIgniter 4, when i click the button, the error message doesn't appear at all, is there something wrong? Please help, I appreciate it
Below is the code:

Controller

public function Daftar()
    {
        // Jika tervalidasi
        if ($this-&gt;validate([
            &#39;nim&#39; =&gt; [
                &#39;label&#39; =&gt; &#39;NIM&#39;,
                &#39;rules&#39; =&gt; &#39;required|is_unique[tbl_anggota.nim]&#39;,
                &#39;errors&#39; =&gt; [
                    &#39;required&#39; =&gt; &#39;{field} Masih Kosong!&#39;,
                    &#39;is_unique&#39; =&gt; &#39;{field} NIM Sudah Terdaftar!&#39;,
                ]
            ],
            &#39;nama&#39; =&gt; [
                &#39;label&#39; =&gt; &#39;Nama&#39;,
                &#39;rules&#39; =&gt; &#39;required&#39;,
                &#39;errors&#39; =&gt; [
                    &#39;required&#39; =&gt; &#39;{field} Masih Kosong!&#39;,
                ]
            ],
            &#39;password&#39; =&gt; [
                &#39;label&#39; =&gt; &#39;Password&#39;,
                &#39;rules&#39; =&gt; &#39;required&#39;,
                &#39;errors&#39; =&gt; [
                    &#39;required&#39; =&gt; &#39;{field} Masih Kosong!&#39;,
                ]
            ],
            &#39;ulangi_password&#39; =&gt; [
                &#39;label&#39; =&gt; &#39;Ulangi Password&#39;,
                &#39;rules&#39; =&gt; &#39;required|matches
		
输入密码查看隐藏内容

&#39;, &#39;errors&#39; =&gt; [ &#39;required&#39; =&gt; &#39;{field} Masih Kosong!&#39;, &#39;matches&#39; =&gt; &#39;{field} Password Tidak Sama!&#39;, ] ], ])) { } else { // Jika tidak tervalidasi session()-&gt;getFlashdata(&#39;errors&#39;, \Config\Services::validation()-&gt;getErrors()); return redirect()-&gt;to(base_url(&#39;Auth/Login&#39;))-&gt;withInput(&#39;validation&#39;, \Config\Services::validation()); } }

View

&lt;?php
            // Notifikasi Error
            $errors = session()-&gt;getFlashdata(&#39;errors&#39;);
            if (!empty($errors)) { ?&gt;
                &lt;div class=&quot;alert alert-danger&quot; role=&quot;alert&quot;&gt;
                    &lt;h4&gt;Periksa Entry Form&lt;/h4&gt;
                    &lt;ul&gt;
                        &lt;?php foreach ($errors as $key =&gt; $error) { ?&gt;
                            &lt;li&gt;&lt;?= esc($error) ?&gt;&lt;/li&gt;
                        &lt;?php } ?&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
            &lt;?php } ?&gt;

Thank you so much!!

I've tried look it up but still it's not working

答案1

得分: 0

你需要在控制器中设置错误时使用[setFlashData()][1]而不是getFlashData(),所以请将以下代码更改为:

..
session()->setFlashdata('errors', \Config\Services::validation()->getErrors());
..
英文:

you need to use setFlashData() instead of getFlashData() while setting error in your controller, so change:

..
session()-&gt;getFlashdata(&#39;errors&#39;, \Config\Services::validation()-&gt;getErrors());
..

to

..
session()-&gt;setFlashdata(&#39;errors&#39;, \Config\Services::validation()-&gt;getErrors());
..

huangapple
  • 本文由 发表于 2023年6月22日 17:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定