Skip to content

Validation rules regex and not_regex don't work with Assets (arrays) #6246

@bradfloodx

Description

@bradfloodx

Bug description

I want to run a regex validation rule on an Assets field type so I can ensure that filenames match a desired pattern.

However, validation always fails on an Asset field type when using the regex or not_regex validation rules because the current logic runs preg_match on the array itself and does not check if the value is an array first (like several of the other validation rules do).

I figured this was a bug instead of a feature request because several other validation rules do array checking and work well with Asset arrays. So it makes sense that we could run regex rules on an array of file paths too.

How to reproduce

  1. Create an Asset field type
  2. Add a regex or not_regex rule
  3. Upload an asset and submit
  4. Validation always fails

Logs

{
    "message": "The given data was invalid.",
    "errors": {
        "story_mp3": [
            "Format is invalid."
        ]
    }
}

Versions

Statamic 3.3.16 Pro
Laravel 8.83.17
PHP 8.0.18
statamic/ssg 1.0.1
swiftmade/statamic-clear-assets 1.1.0
withcandour/aardvark-seo 2.0.27

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

No response

Additional details

Statamic is the best!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions