-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
L8.15.0 - Validation for RFC3339_EXTENDED date format doesn't work #35387
Comments
The validator just uses |
@driesvints |
@vlauciani although both dates are created with no errors when calling If you run: echo DateTimeInterface::RFC3339_EXTENDED; You will get
In PHP docs' date format page where they list all the format tokens
Reference: https://www.php.net/manual/en/datetime.format.php So I don't know why But, in my opinion, it is not a bug in how Laravel handles this validation. Actually Laravel's validation for date and time format is pretty straightforward: framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php Lines 391 to 393 in ca0b82a
Just for the record I tested without the leading exclamation mark and it still fails validation with the date ending in Z. As a workaround, if you need to accept both formats you could try a custom rule: <?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Str;
use Illuminate\Validation\Concerns\ValidatesAttributes;
class DateFormat implements Rule
{
use ValidatesAttributes;
private string $format;
public function __construct(string $format)
{
$this->format = $format;
}
public function passes($attribute, $value)
{
if ($this->format === \DateTimeInterface::RFC3339_EXTENDED && Str::endsWith($value, 'Z')) {
$value = Str::replaceLast('Z', '+00:00', $value);
}
return $this->validateDateFormat($attribute, $value, [$this->format]);
}
public function message()
{
return Str::replaceFirst(':format', $this->format, \trans('validation.date_format'));
}
} The use it as: $array = [
'mydate1' => '2018-01-29T20:36:01.123Z',
'mydate2' => '2018-01-29T20:36:01.123+00:00',
];
$validator = \Illuminate\Support\Facades\Validator::make($array, [
'mydate1' => ['required' , new \App\Rules\DateFormat(\DateTimeInterface::RFC3339_EXTENDED) ],
'mydate2' => ['required' , new \App\Rules\DateFormat(\DateTimeInterface::RFC3339_EXTENDED) ],
])->validate();
return 'ok'; Hope this helps. |
Thank you very much @rodrigopedra |
Description:
The validation for RFC3339_EXTENDED PHP date format, doesn't work correctly.
Steps To Reproduce:
Both
mydate1
andmydate2
are a valid RFC3339_EXTENDED PHP date format:but calling the
test_date
route on your browser, Laravel Validation return an error onmydate1
.The text was updated successfully, but these errors were encountered: