Skip to content

Rule validation for JSON throws TypeError on PHP 8.0 #35795

@Joorren

Description

@Joorren
  • Laravel Version: 8.21.0 (latest)
  • PHP Version: 8.0.0
  • Database Driver & Version: MySQL 5.7

Description:

When using the 'JSON' rule validation in PHP 8, you will get a TypeError when submitting an empty field:

method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
in vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:1149

According to php.watch:
Union types are used when they are deemed necessary. For example, method_exists() function accepts either a class name (string) or an object (object). This is enforced as a Union Type of string|object. [...] Prior to PHP 8, it returned false if the provided parameter is not a string or an object, but did not raise any warnings.

Steps To Reproduce:

  1. Create a form with an input field that contains no value (hidden in my case).
  2. Add a json rule for this field (json|required in my case).
  3. Submit the form.
    The website will return method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions