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',
];
}
Related
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;
}
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']),
];
}
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]);
}
I am making a API request but when i retrieve created_at then i get 2019-10-09T11:07:08.000000Z but in database it is 2019-10-09 11:07:08. How can i solve this issue? I don't want to use date formatting because i already have proper date format in my database. I just want what i have in database. Look at my database: https://prnt.sc/piljms
API Controller:
public function singleGallery($id)
{
$gallery = Gallery::findOrFail($id);
return new GalleryResource($gallery);
}
Gallery Resource:
return [
'gallery_id' => $this->id,
'created_date' => $this->created_at,
'modified_date' => $this->updated_at
]
Using php's date and strtotime function you can format date as per your choice.
'created_date' => date('Y-m-d', strtotime($this->created_at))
I have formatted created_at in Year/month/date but you can do different formatting like d/m/Y for day/month/Year
If you would like to go Laravel way then you have to use Carbon library.
Go with this:
return [
'gallery_id' => $this->id,
'created_date' => $this->created_at->format('Y-m-d H:i:s'),
'modified_date' => $this->updated_at->format('Y-m-d H:i:s'),
]
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
class Notification extends Model
{
use HasFactory;
protected $fillable = [
'id',
'created_at',
'updated_at',
];
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}
?>
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',
];
}