Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Print errors in array validations #3917

Closed
luispastendev opened this issue Nov 23, 2020 · 10 comments
Closed

Bug: Print errors in array validations #3917

luispastendev opened this issue Nov 23, 2020 · 10 comments

Comments

@luispastendev
Copy link

Hello I am trying to print the errors when I validate an array structure for example:

<select multiple name="categories[]">
# validation rules
 'categories.*' => 'is_not_unique[categories.id]'

# return errors
return redirect()->back()
                ->with('errors', $this->validator->getErrors())

then I try to print the errors in my html but it cannot show them, I suppose it is because of the key format that the function to obtain the errors returns

<?=session('errors.categories.*')?>

I think it is not the most practical way to return errors when it comes to array validations :(

thanks for everything!

@paulbalandan
Copy link
Member

If you use <?= print_r(session('errors.categories'), true) ?> will there be outputs?

@luispastendev
Copy link
Author

Hello Paul!

try to print it that way but a null value is returned, the format of the array returned in session is as follows

array (5) {
//    ...
     ["categories.*"] =>
   string (80) "The categories.* field must contain a previously existing value in the database."
}

It would be quite nice if the ". *" Could be omitted in the name of the keys when dealing with array validations, or how could I get the error message?

Cheers!

@paulbalandan
Copy link
Member

Try setting a label to your validation rules:

// validation rules
'categories.*' => ['rules' => 'is_not_unique[categories.id]', 'label' => 'categories'],
// The categories field ...

But note that the array key will still be categories.* since this is the field name passed to the errors array.

@luispastendev
Copy link
Author

It seems that the name of the label is not associated with the name of the error key because I have tried to put a label to the configuration and it keeps returning categories.*

'categories.*' => [
    'rules' => 'is_not_unique[categories.id]',
    'label' => 'categories',
],

imagen

as I mentioned before if I try to print the error as it returns null

@michalsn
Copy link
Member

michalsn commented Jan 7, 2021

It seems like a bug related to fact that we're using dot syntax to handle the nested array values.

Until we solve this I guess you should be able to use something like this:

<?= session('errors')['categories.*'] ?>

@paulbalandan
Copy link
Member

But note that the array key will still be categories.* since this is the field name passed to the errors array.

It seems that the name of the label is not associated with the name of the error key because I have tried to put a label to the configuration and it keeps returning categories.*

'categories.*' => [
    'rules' => 'is_not_unique[categories.id]',
    'label' => 'categories',
],

imagen

as I mentioned before if I try to print the error as it returns null

Yes, as I've said earlier, the field name will still be used as the array key.

This is because of the following line in Validation::processRules.

$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value)

The $field is used as the index for storing the error messages in the $this->errors array.
@michalsn The simple fix I see is using rtrim on $field before being used as index, like $this->errors[rtrim('$field', '.*')] = .. but I'm not sure if this will not cause a confusion because categories, or any other multiple-value fields, will look like a single-value field much like the other fields.

@michalsn
Copy link
Member

michalsn commented Jan 7, 2021

@paulbalandan Yes, this might be confusing and a little too "magical". Also it would introduce a breaking compatibility, since this bug is valid only if we want to display errors via session.

I was thinking more about adding some new (optional) value to validation array, like:

'categories.*' => [
    'rules' => 'is_not_unique[categories.id]',
    'label' => 'Categories',
    'as' => 'categories',
],

With a note in the user guide that as should contain only alpha numeric value with dash or underscore. This way everything would be easier to understand. But these are just my initial thoughts.

There is a workaround for this bug, so it's nothing urgent, I think.

@luispastendev
Copy link
Author

It seems to be a good idea from michals, although it is not something urgent to solve since it can be solved with this <?= session('errors')['categories.*'] ?>, by the way, nice that you are here again @michalsn :)

@paulbalandan paulbalandan added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 9, 2021
@kenjis kenjis changed the title Print errors in array validations Bug: Print errors in array validations Jan 6, 2022
@kenjis
Copy link
Member

kenjis commented Jul 8, 2022

Since v4.2 getErrors() returns like this:

$this->validation->withRequest($request->withMethod('post'))->run();
$this->assertSame([
'id_user.0' => 'The id_user.* field must contain only numbers.',
'name_user.0' => 'The name_user.* field may only contain alphabetical characters.',
'name_user.2' => 'The name_user.* field may only contain alphabetical characters.',
'contacts.friends.0.name' => 'The contacts.*.name field is required.',
], $this->validation->getErrors());

Ref #5609

So it seems session('errors')['categories.*'] does not work.

$validation->getError('categories.*') returns all found errors that will be combined into one line separated by the EOL character:

$this->assertSame(
"The name_user.* field may only contain alphabetical characters.\n"
. 'The name_user.* field may only contain alphabetical characters.',
$this->validation->getError('name_user.*')
);

@kenjis
Copy link
Member

kenjis commented Jul 8, 2022

session('errors.categories.*') searches an item with dot notation, and there is no such key in the array.
Because the array key is categories.* (at the time the bug was reported), and dot_array_search() does not find such a key. So it returns null.

So this does not seem a bug.

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 8, 2022
@kenjis kenjis closed this as completed Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants