Validation does not appear |
I was facing an issue where the validation doesn't appear. The code should be make the `form-control` had the `is-invalid` class when there is an error. In my case, the validation appear on the "Session User Data", but when I check the "View Data", the validation doesn't appear on the "protected errors" (As far as I remember, it should be there).
Controller: PHP Code: <?php Views: Code: <form method="post" action="<?= base_url('auth/save'); ?>"> Screenshot: ![]() ![]() ![]()
Your code doesn't explicitly show where the validation object comes from in your view. I'm not sure which version of CodeIgniter you're using, but at some point I recall that one or more of the validation methods changed. The current prescribed technique for validation from the docs:
PHP Code: // In Controller.
It sounds like you're using CodeIgniter 4 and facing an issue where validation errors appear in session flashdata but not in the view’s `$validation->getError()` output.
✅ Possible Causes and Fixes: 1. **Check if `$validation` is passed to the view** Make sure your controller passes the `Validation` service or flash data explicitly: ```php $data['validation'] = \Config\Services::validation(); return view('your_view', $data); ``` If using redirect with `withInput()`, in the view, use: ```php $validation = \Config\Services::validation(); ``` #### 2. **Ensure Validation Errors Exist in `session()->getFlashdata('_ci_validation_errors')`** Use this in the view to debug: ```php <?= session()->getFlashdata('_ci_validation_errors') ?> ``` If it returns null, the error might not be carried with `withInput()`. #### 3. **In your Form Field** Make sure you are checking errors correctly: ```php <input type="text" name="username" class="form-control <?= ($validation->hasError('username')) ? 'is-invalid' : '' ?>"> <div class="invalid-feedback"> <?= $validation->getError('username'); ?> </div> ``` #### 4. **Check CSRF or Form Submission Flow** If you're redirecting after form post, ensure `withInput()` is chained: ```php return redirect()->back()->withInput()->with('errors', $validation->getErrors()); ``` --- Let me know if you'd like to share your view and controller code—I can help debug line-by-line.
Need a local USA or Canada Number? Visit https://nowtext.app/ and book one now.
(06-29-2025, 09:01 PM)grimpirate Wrote: Your code doesn't explicitly show where the validation object comes from in your view. I'm not sure which version of CodeIgniter you're using, but at some point I recall that one or more of the validation methods changed. The current prescribed technique for validation from the docs: It may work, but now the URL is not on the `register` anymore, it's on `save`, since the controller to handle the request is on `save`, not on the `register`. The thing is, I want the URL to stay on `register` while displaying the errors from validation. ![]()
Here is a demonstration of CodeIgniter 4.6.1 with Shield. It functions as it should (leaves the user on /register). Perhaps start from there and modify to do whatever it is you're trying to achieve, which I'm guessing is that you want the error messages displayed above the field it pertains to? If that's the case all you have to do is modify the registration view.
|
Welcome Guest, Not a member yet? Register Sign In |