Laravel date validation literal 'after' value - php

I have created a form request file which validates my start_date form input.
public function rules()
{
return [
'start_date' => 'required|date|after:2017-06-31',
];
}
I have placed a literal value in the after value, this however doesn't look like its working. Can someone advise the best way to do this?
Edit:
This is the dump from my $request->all();
{
"_token": "tZa4e39ejrGHtrlpOYrUPfZ8PgSeD8FelY4voKni",
"start_date": "2017-07-01"
}
This is my form request validation:
public function rules()
{
return [
'start_date' => 'required|date_format:Y-m-d|after:2017-06-31',
];
}
I am using Laravel v5.4

The dates will be passed into the PHP strtotime function so change
your date format to Y-m-d .
public function rules()
{
return [
'start_date' => 'required|date|date_format:Y-m-d|after:2017-06-31',
];
}

Related

Creating two seperate validation messages for the same attribute

I'm trying to create two separate validation messages for the same validation attribute.
There are two rules that use "before_or_equal" - the end_time has to be "before_or_equal" start_time and also the end_time has to be after 5:00 (time). The validation works, but I can't seem to find a way to create a working custom message for the latter.
I tried to specify the rule by including it literally with the value, but it doesn't seem to work.
This is what the custom request validation looks like for the end_time at the moment.
public function rules()
{
return [
'end_time' => ['after_or_equal:start_time', 'after_or_equal:5:00'],
];
}
public function messages()
{
return [
'end_time.after_or_equal' => 'Message 1',
'end_time.after_or_equal:5:00' => 'Message 2',
];
}
You can use :date for your custom error messages.
Example:
public function rules()
{
return [
'end_time' => ['after_or_equal:start_time', 'after_or_equal:5:00'],
];
}
public function messages()
{
return [
'end_time.after_or_equal' => 'the :attribute time must be after :date',
];
}
The replaced value is actual value of first input of the validator
i don't know if i understand your question correctly, but are you looking for something like this ?
public function rules()
{
return $this->messages("end_time", [
"after_or_equal",
"after_or_equal:5:00",
]);
}
public function messages(string $Key, array $CustomAttributes)
{
$Exceptions = [
"end_time" => [
"after_or_equal" => "Message 1",
"after_or_equal:5:00" => "Message 2"
]
];
$Exception = [
$Key => []
];
foreach ($CustomAttributes as $Attribute) {
array_push($Exception[$Key], $Exceptions[$Key][$Attribute]);
}
return $Exception;
}

Laravel validation form request doesn't work for conditions rule with a date field

I have a problem with the form request validation rules. I need to check additional data when the input date is less than 18 years ago (the user is not an adult). I write rules but it doesn't work. This is my code:
$adultDate = Carbon::now()->subYears(18)->format('Y-m-d');
$rules = [
"brith_date" => 'required|date',
"patron_name" => "required_if:brith_date,after,".$adultDate."|string"
];
return $rules;
you can use Rule::when($condition, $rules)
<?php
use Illuminate\Validation\Rule;
public function rules()
{
$adultDate = Carbon::now()->subYears(18);
$condition=Carbon::parse($this->brith_date)->isAfter($adultDate);
return [
'brith_date' => ['required','date'],
'patron_name' => ['required' ,
Rule::when($condition, ['string']),
];
}

Problem with updating date from form to database

I am trying to update date_of birth column in database and when I submit my form I get this error
DateTime::__construct(): Failed to parse time string (25/03/1995) at position 0 (2): Unexpected character
Now in my blade I formated date of birth to show d/m/Y and when updating I think it updates Y/m/d, because when I remove format function from my blade it works fine. So I need help on how to update with format('d/m/Y') in my database and how to validate it properly in my form request validation. Any help is appreciated. Here is my code.
index.blade.php
<input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth ? $userForShowProfile->date_of_birth->format('d/m/Y') : "" }}">
UserController.php
public function updateProfileCharacteristics(UpdateProfileCharacteristicsRequest $request)
{
$user = Auth::user();
$user->update(
[
'date_of_birth' => $request->date_of_birth,
'age' => Carbon::now()->diffInYears($request->date_of_birth),
'updated_at' => Carbon::now()
]
);
return redirect()->route('profile.show', [$user->username]);
}
UpdateProfileCharacteristicsRequest.php
public function rules()
{
return [
'date_of_birth' => ['date'],
];
}
Since you are sending the date in a custom format in the request, you will need to parse it to a format that matches the one in the database column before inserting it:
$user->update(
[
'date_of_birth' => Carbon::createFromFormat("d/m/Y", $request->date_of_birth)->format('Y-m-d'), // parse the right format here
'age' => Carbon::now()->diffInYears(Carbon::createFromFormat("d/m/Y", $request->date_of_birth)),
'updated_at' => Carbon::now()
]
);
And for that date format to pass validation you can use the date_format:format rule instead of date:
public function rules()
{
return [
'date_of_birth' => ['date_format:"d/m/Y"'],
];
}
What is the column type in you database migration? If it is check if it DATE or DATETIME or TIMESTAMP, It is supposed to be DATE hence you can format your date to be Y-m-d.
If you are to save a DATE to DB it should be in the format of Y-m-d.
so try this:
public function updateProfileCharacteristics(Request $request)
{
$user = Auth::user();
$user->update([
'date_of_birth' => Date('Y-m-d',strtotime($request->date_of_birth)),
'age' => Carbon::now()->diffInYears($request->date_of_birth),
'updated_at' => Carbon::now()
]);
return redirect()->route('profile.show', [$user->username]);
}

Laravel 5.2 validation fails when parameters value missing or equal symbol missing

Have a request
/web/v1/documents/?date=2019-05-22&sort=category
contrtoller:
$sort = $request->get('sort', 'commodity');
This value is optional and here validation:
DocumentsReportRequest:
public function rules()
{
return [
'sort' => ['in:commodity,category'],
];
}
if request
/web/v1/documents/?date=2019-05-22&sort
or
/web/v1/documents/?date=2019-05-22&sort=
Validation rule does not work and i got error.
Is there a way to validate parameters in this case?
You can add nullable or sometimes rule to your validation, like this:
public function rules()
{
return [
'sort' => ['nullable', 'in:commodity,category'],
];
}
Or present which requires the key to exist in the request but it can be empty.
You can use nullable validation if the parameter is not always available
public function rules()
{
return [
'sort' => ['nullable','in:commodity,category'],
];
}

How to use m/d/Y date format Laravel 5.6

I am using Laravel 5.6 and i need to add birthday with m/d/Y format but get date error. Example for this value '09-20-2018'.
The birthday is not a valid date.
public function rules()
{
return [
'birthday' => 'required|date|date_format:m-d-Y',
];
}
You should try this:
public function rules()
{
return [
'birthday' => 'date_format:m/d/Y|required|date',
];
}

Categories