Skip to content

Before/After validation rules ignore the validation state of the dependent field #49863

Closed
@naquad

Description

@naquad

Laravel Version

10.34.2

PHP Version

8.3.2

Database Driver & Version

No response

Description

Validator's before and after rules are trying to construct the second operand from non-validated potentially invalid data (ValidatesAttributes::compareDates() is retrieving the data using Validator::getValue() which returns raw data). This causes unexpected exceptions.

Steps To Reproduce

Sample script

<?php

require_once(__DIR__ . '/vendor/autoload.php');
$app = require __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use Illuminate\Support\Facades\Validator;

$data = [
    'dt1' => ['this' => ['is' => ['a' => ['mess']]]],
    'dt2' => 'some invalid date',
];

$val = Validator::make($data, [
    'dt1' => 'required|date',
    'dt2' => 'required|date|before:dt1',
]);

$val->validate();

Output


   TypeError 

  DateTime::__construct(): Argument #1 ($datetime) must be of type string, array given

  at vendor/nesbot/carbon/src/Carbon/Traits/Creator.php:89
     85▕             setlocale(LC_NUMERIC, 'C'); // @codeCoverageIgnore
     86▕         }
     87▕ 
     88▕         try {
  ➜  89▕             parent::__construct($time ?: 'now', static::safeCreateDateTimeZone($tz) ?: null);
     90▕         } catch (Exception $exception) {
     91▕             throw new InvalidFormatException($exception->getMessage(), 0, $exception);
     92▕         }
     93▕ 

      +13 vendor frames 

  14  poc.php:19
      Illuminate\Validation\Validator::validate()

Expected result

Validation exception stating that both values are invalid.

Actual result

TypeError fatal error.

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