DEV Community

waziri ally amiri
waziri ally amiri

Posted on

Why you should never use the date validation rule without the data_format rule in Laravel

Hellow fellow Artisan,

The date validation rule in Laravel checks that a field is a valid date. However, it does not specify the format of the date. This can lead to errors if the user enters the date in a different format than expected.

Example if user enter incorrect date like this 20000-02-20 where instead of four digits(2000) for year five digit(200000) set , this will cause your application to break with this error below.
Image description

To avoid your application from break , you should avoid using only date rule like
'item.date_of_birth' => 'required|date|before:' .today()->subYears(7)->format('Y-m-d')
but you must use it with date_format() rule like this

'item.date_of_birth' => 'required|date|date_format:Y-m-d|before:' .today()->subYears(7)->format('Y-m-d')
so that instead of your application to break, user will get the validation error.

Happy Coding !


About the Author
Hi there, I'm a Full Stack Developer based in Tanzania with a passion for Laravel, Livewire, VueJs, TailwandCss, and web development. As a freelancer, I'm always ready to take on exciting projects and collaborate on remote work related to Laravel.

If you're looking for a dedicated Laravel developer who can help you bring your web application to life or enhance your existing Laravel project, feel free to reach out to me. I'm eager to work with you to create robust and innovative solutions for your business needs.

You can contact me at [wazirially1994@gmail.com] or connect with me at [https://github.com/WAZIRI123] and WhatsApp via:[+255653039317].

Let's turn your Laravel project into a success story together!

Top comments (0)