0
votes

I'm using laravel 5.5 with MySQL database.

Here is my current code:

private function setValidationRule(Request $request,$actionCode) {
    $rules = [];
    $rules['odaydate'] = 'required|unique:empofday|date_format:"d-m-Y"';
    return $rules;
}

public function postCreate(Request $request) {
    //Previous code...

    $validator = \Validator::make($request->all(),
        $this->setValidationRule($request,$actionCode),
        $this->setCustomValidationRuleMsg($request,$actionCode)
    );
    $validator->setAttributeNames($this->setValidationAttributeNames());
    if ($validator->fails()) {
        $view = $viewPri->withErrors($validator)->withInput();
    } else {
        $view = $viewSec;
    }
    return $view;
}

The posting data from client is DD-MM-YYYY = '15-10-2018'.

Then i want to validate the date uniqueness with the rule :
$rules['odaydate'] = 'required|unique:empofday|date_format:"d-m-Y"';

Yes the validation is true (success) but the laravel generated sql was wrong to get the uniqueness :
select count(*) as aggregate from empofday where odaydate = '15-10-2018'
so the resulting sql would be 0 instead of 1.

But if i try manual sql with the following code :
select count(*) as aggregate from empofday where odaydate = '2018-10-01'
resulting value 1.

So, my question is how to set date format for laravel validating uniqueness on database to use d-m-Y format instead of Y-m-d?

Thanks for the help.

1
which database are you using? - usmanhaq
Ow yeah forgot to tell, i'm using MySQL. I will update my question. - Zemmy Indrapatih

1 Answers

0
votes

If you are using simple text box for date input, I recommend to change that to some date picker plugin, the plugins do have functionality to have a date format that is displayed different than what is sent back to the database.

After that you can remove the date format validation, because it will be managed by the plugin, and unique will run because the format sent by the plugin can be set to be in MySQL format (YYYY-MM-DD)

For jQuery UI Date picker http://api.jqueryui.com/datepicker/#option-altFormat