laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.45k stars 11k forks source link

date_format|after with relative date does not work #53145

Closed Superpat closed 2 weeks ago

Superpat commented 2 weeks ago

Laravel Version

11.8

PHP Version

8.3

Database Driver & Version

No response

Description

The following validation does not work: date_format:m/y|after:today but date_format:m/y|after:12/25 does work.

If I pass 12/22 to the first one it's marked as being a valid date, but it should not. The non relative date correctly detects it as non valid.

Steps To Reproduce

A controller, action and request are all required before a test can be written. Though more advanced php dev's might be able to write a unit test for this that only uses a request class or the validation string. Here's a more or less complete example.

use Illuminate\Foundation\Http\FormRequest;

class DateValidationRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'workingDateVal' => 'required|date_format:m/y|after:12/25',
            'brokenDateVal' => 'required|date_format:m/y|after:today',
        ];
    }
}
use App\Http\Requests\DateValidationRequest;

class ValidateDate
{
    public function handle(DateValidationRequest $request): string
    {
       return $request->workingDateVal;
    }
}
use App\Actions\ValidateDateAction;
use App\Http\Requests\DateValidationRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class ValidateDateController extends Controller
{
    public function validateDate(ValidateDateRequest $request, ValidateDateAction $action): string
    {
        return $action->handle($request);
    }
}

    Route::prefix('validate-date')->group(function () {
        Route::post('please', [ValidateDateController::class, 'validateDate']);
    });

test('validateDate returns validation errors', function () {
    posttJson('/validate-date/please', [
        'workingDateVal' => '12/22',
        'brokenDateVal' => '12/22'
    ]) ->assertInvalid([
            'workingDateVal' => 'The working date val must be a date after 12/25.'
            'brokenDateVal' => 'The broken date val must be a date after today.'
        ]);
});
hafezdivandari commented 2 weeks ago

Works as expected? https://laravelplayground.com/#/snippets/522d7490-13c7-4d21-b977-aa62b7d68b86

crynobone commented 2 weeks ago

Hi there,

Thanks for reporting the problem you are encountering, but it looks like this is a question which may be better suited for a support channel. We only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repository you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.