AlexB
AlexB

Reputation: 2174

Date Validation and date database insertion in Laravel 5

I have a promo_starts_at and promo_ends_at fields in DB with type date The date is entered via bootstrap date-picker in the following format 01/20/2019

  "promo_starts_at" => "01/22/2019"
  "promo_ends_at" => "01/30/2019"

I have the following set of rules in my ProductManager:

if($parameters['type'] == 'integer'){ $data[$field] = intval($request->input($field)); }
if($parameters['type'] == 'string'){ $data[$field] = $request->input($field); }
if($parameters['type'] == 'decimal'){ $data[$field] = $request->input($field); }
if($parameters['type'] == 'text'){ $data[$field] = $request->input($field); }
if($parameters['type'] == 'bool'){ $data[$field] = $request->input($field) ? 1 : 0; }
if($parameters['type'] == 'date'){ $data[$field] =  date('Y-m-d', strtotime($request->input($field))); }

Additionally, I also have a set of validation rules:

    'promo_starts_at' => [
        'type' => 'date',
        'validation' => 'date'
    ],
    'promo_ends_at' => [
        'type' => 'date',
        'validation' => 'date'
    ]

I cannot post the date into the database for some reason.

I have also tried different validation rules, such as date_format:Y-m-d|after:today, but nothing changes.

I have found a fix for this problem, the last rule should be changed to the following:

$data[$field] =  date('Y-m-d', strtotime($request->input($field)));

This has fixed the problem for me.

Upvotes: 1

Views: 27

Answers (1)

AlexB
AlexB

Reputation: 2174

I have found a fix for this problem, the last rule should be changed to the following:

$data[$field] =  date('Y-m-d', strtotime($request->input($field)));

This has fixed the problem for me.

Upvotes: 0

Related Questions