Laravel 5.1 Date_Format Validation Allow Two Formats

Laravel 5.1 date_format validation allow two formats

The date_format validator takes only one date format as parameter. In order to be able to use multiple formats, you'll need to build a custom validation rule. Luckily, it's pretty simple.

You can define the multi-format date validation in your AppServiceProvider with the following code:

class AppServiceProvider extends ServiceProvider  
{
public function boot()
{
Validator::extend('date_multi_format', function($attribute, $value, $formats) {
// iterate through all formats
foreach($formats as $format) {

// parse date with current format
$parsed = date_parse_from_format($format, $value);

// if value matches given format return true=validation succeeded
if ($parsed['error_count'] === 0 && $parsed['warning_count'] === 0) {
return true;
}
}

// value did not match any of the provided formats, so return false=validation failed
return false;
});
}
}

You can later use this new validation rule like that:

'trep_txn_date' => 'date_multi_format:"Y-m-d H:i:s.u","Y-m-d"' 

You can read more about how to create custom validation rules here: http://laravel.com/docs/5.1/validation#custom-validation-rules

How to validate date time with many format in laravel

You must write a custom validation format for that. Laravel's date_format expects only one parameter and not capable of handling multi-formats. There are Two ways to add custom validation. first, one is making a rule repository and add your validation logic there. Here Taylor Otwell explained this method.

The other way to doing that is extend validation in app service provider and add new rule there. add this code in app service provider:

use Illuminate\Support\Facades\Validator;

Validator::extend('multi_date_format', function ($attribute, $value, $parameters,$validator) {

$ok = true;

$result = [];

// iterate through all formats
foreach ($parameters as $parameter){

//validate with laravels standard date format validation
$result[] = $validator->validateDateFormat($attribute,$value,[$parameter]);
}

//if none of result array is true. it sets ok to false
if(!in_array(true,$result)){
$ok = false;
$validator->setCustomMessages(['multi_date_format' => 'The format must be one of Y-m-d ,Y-m or Y-m-']);
}

return $ok;
});

And here you can use it this way:

$validator = Validator::make(['date' => '2000-02-01'], [
'date' => 'required|multi_date_format:Y-m-d,Y-m,Y-m-'
]);

if($validator->fails()) {
$errors = $validator->errors()->all();
}

Laravel Eloquent date_format Validation fails on given format

If you want to be able to pass the day without a leading zero, then for the day part of your datetime you need to use j instead of d.

'date' => 'required|date_format:"Y.m.j H:i:s"',

That will work for the example you have above.

If you are going to always have a leading zero in your day (so, 05 instead of just 5, then the date format you already have will work.

Laravel 5.1 Date_Format Validator Fails on Y-m-d Format

Apparently, the date_format validation failed as I randomly chose 2015-02-30 to test my API, but that day doesn't exist as that would be February 30... oh the shame and the wasted time. Thanks to @ceejayoz for all the help!!!

L5.6 - Validate multiple 'rules' in OR

I found a solution, using the Rule Objects:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

use Illuminate\Support\Facades\Validator;

class StartOrEndDateRule implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$return1 = true;
$return2 = true;

/* Check validation on 'YYYY-MM-DD\THH:ii:ss' */
$validator = Validator::make([$attribute => $value], [
$attribute => 'date_format:"Y-m-d\TH:i:s"',
]);
if ($validator->fails()) {
$return1 = false;
}

/* Check validation on 'YYYY-MM-DD' */
$validator = Validator::make([$attribute => $value], [
$attribute => 'date_format:"Y-m-d"',
]);
if ($validator->fails()) {
$return2 = false;
}

/* Check value to return */
if ( $return1 === false && $return2 === false ) {
return false;
} else {
return true;
}
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The ":attribute" does not match the format "Y-m-d\TH:i:s" or "Y-m-d"';
}
}

and in the Controller:

$validator = Validator::make($input_data, [
'starttime' => [new StartOrEndDateRule],
]);


Related Topics



Leave a reply



Submit