Laravel 5.2 validation: date_format: Ymd not working

I am trying to check the POST request.

Format: dmY (12.1.2017) My rule is required|date_format:dmY for this field.

I get this error message:

 InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. Data missing 

If I changed the value . on - or even / , it works -> POST data has changed before it matches the rule.

I need a German format for this.

edit: My validation rules:

 public function rules() { return [ 'title' => 'required|max:255', 'expiration_date' => 'required|date_format:dmY', //'description' => 'required', 'provision_agent' => 'required|integer|between:0,100', 'discount_consumer' => 'required|integer|between:0,100', 'quota' => 'required|integer', ]; } 
+5
source share
4 answers

Wrap your format, which should work, I just tried with 5.2 to work fine.

 public function rules() { return [ 'title' => 'required|max:255', 'expiration_date' => 'required|date_format:"dmY"', //'description' => 'required', 'provision_agent' => 'required|integer|between:0,100', 'discount_consumer' => 'required|integer|between:0,100', 'quota' => 'required|integer', ]; } 

But the error you added to the InvalidArgumentException question in Carbon.php line 425: there seems to be something else, I assume that you are using expiration_date , where in the controller or model like this with Carbon

  echo Carbon::createFromFormat('Ym-d', '12.1.2017'); 

You should try something like this

 echo Carbon::parse('12.1.2017')->format('Ym-d') 
+4
source

Paste your format in quotation marks:

 'date_format:"dmY"' 
0
source

Try it,

 public function rules() { return [ 'title' => 'required|max:255', 'expiration_date' => 'date_format:"dmY"|required', // I have changed order of validation //'description' => 'required', 'provision_agent' => 'required|integer|between:0,100', 'discount_consumer' => 'required|integer|between:0,100', 'quota' => 'required|integer', ]; } 

Hope this solves your problem.

0
source

If you cannot solve the problem otherwise, you can use the custom validation rule :

 Validator::extend('date_dmY', function ($attribute, $value) { $format = 'dmY'; $date = DateTime::createFromFormat($format, $value); return $date && $date->format($format) === $value; }, 'optional error message'); 

An additional check for $date->format($format) === $value is to avoid mistakenly accepting dates outside the range, for example. "1/32/2017". See this comment on php.net.

Once a custom validation rule has been defined, you can use it like this:

 public function rules() { return [ 'expiration_date' => 'required|date_dmY', ]; } 
0
source

Source: https://habr.com/ru/post/1262699/


All Articles