1

I'm trying to validate an array of files I have in Laravel and Livewire v3. I've written up the validation rules for my properties and hit a roadblock here:

#[Validate(["files" => ["array", "max:5"], "files.*" => ["image", "mimes:jpg,bmp,png"]])]
public array $files = [];

The validation works fine, however I am struggling on changing the error message to use an alias on array elements that have been validated:

The files.0 field must be a file of type: png, jpg.

Is the message I get, but I am trying to change it to:

The file field must be a file of type: png, jpg.

I've managed to achieve that when not validating an array like this:

#[Validate(["required", "string", "min:10"], as: "question")]
public ?string $question;

I've also tried changing the validation attributes like this, too:

protected array $validationAttributes = [
    "files.0" => "file",
    "files.1" => "file",
    "files.2" => "file",
    "files.3" => "file",
    "files.4" => "file"
];

but can't get it to work on files mentioned above.

Does anyone know if this is possible, or should I rewrite validation to be like it was in Livewire v2?

EDIT: I've done some experimentation as suggested by one of the answers:

#[Validate(["files.*" => ["image", "mimes:jpg,bmp,png"]], message: "The file field must be a file of type: png, jpg")]
public array $files = [];

Seems to throw an Array to string conversion exception if "files.*" validation array has more than one rule.

After reducing the validation rules to just mimes, the exception went away, but the message still was not applied.

2
  • 1
    as you did an array for rules do the same for message message: ["the file must an image", "The file field must be a file of type: png, jpg"] Commented Jun 20, 2024 at 12:51
  • 1
    @Erikas check the answer again, it is updated now Commented Jun 20, 2024 at 12:56

1 Answer 1

1

You could achieve that like this by editing the validation message

#[Validate(["files" => ["array", "max:5"], "files.*" => ["image", "mimes:jpg,bmp,png"]], message: ['The file field must be a file of type: png, jpg'])]
public array $files = [];

or for more cleaner way you could write it like this

#[Validate(["files" => ["array", "max:5"]])]
#[Validate(["files.*" => ["image", "mimes:jpg,bmp,png"]], message: ['The file field must be a file of type: png, jpg'])]
public array $files = [];

for more info check Custom validation message

Sign up to request clarification or add additional context in comments.

2 Comments

I've tested this out just now, and it seems that if you use message with an array of things you are validating like this: ["files.*" => ["image", "mimes:jpg,bmp,png"]] you will get an array to string conversion exception. And when reducing the validation rules to just ["files.*" => ["mimes:jpg,bmp,png"]] the exception goes away, but the message still seems to get ignored, and not used.
Fixed, i update the message to accept array instead of string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.