Welcome Guest, Not a member yet? Register   Sign In
CI4 validation, obtain an error on a field other than the controlled one
#1

(This post was last modified: 10-17-2022, 08:34 AM by serialkiller.)

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
Reply
#2

(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.
Reply
#3

(This post was last modified: 10-18-2022, 12:41 AM by serialkiller.)

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB