CodeIgniter Forums
CI4 validation, obtain an error on a field other than the controlled one - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CI4 validation, obtain an error on a field other than the controlled one (/showthread.php?tid=84165)



CI4 validation, obtain an error on a field other than the controlled one - serialkiller - 10-17-2022

Using validation, if a field does not match the rules, the specific field name is reported to show the error.
In my case, I retrieve the error array to report errors under the affected fields.

In some cases I have to check if at least one element has been checked, inserted, combined, etc., etc.
In these cases I have to report the error not under a field, but under a certain block of elements.
If the field or fields were named "foo" or "foo[]" if they are for example a group of checkboxes, the error would be reported in the wrong places (at least for my needs).
Is there any way, direct or indirect, to check the "foo" field, but report the error in the "bar" element?

So if I check the rules for the "foo" field, and I have errors, return me in the array not foo but bar


RE: CI4 validation, obtain an error on a field other than the controlled one - kenjis - 10-17-2022

(10-17-2022, 08:34 AM)serialkiller Wrote: Is there any way, direct or indirect, to check the "foo" field, but report the error in the "bar" element?

No.
In that case, you should think that the error is in the "bar" element, not in the "foo" field.


RE: CI4 validation, obtain an error on a field other than the controlled one - serialkiller - 10-18-2022

(10-17-2022, 02:26 PM)kenjis Wrote:
(10-17-2022, 08:34 AM)serialkiller Wrote: Is there any way, direct or indirect, to check the "foo" field, but report the error in the "bar" element?

No.
In that case, you should think that the error is in the "bar" element, not in the "foo" field.

Hi kenjis, my problem is that bar is not a field but a div

i tried this way, it works, but i don't know if it's the best way

PHP Code:
$errors $this->model->errors();

if (
$this->model->validation->hasError('foo')) {
    $errors['bar'] = $errors['foo'];
    unset($errors['foo']);


If I have an error on the foo field, I change the errors array key from foo to bar