Now, let's try to add one more field to our form and database: due_date with a date picker.
Prepare Database/Controller
I will just show you the code without any comments. These are Laravel fundamentals, I'm sure you know them.
Migration:
php artisan make:migration add_due_date_to_tasks_table
Schema::table('tasks', function (Blueprint $table) { $table->date('due_date')->nullable();});
Then, we run:
php artisan migrate
Adding to Model fillables:
app/Models/Task.php:
class Task extends Model{ use HasFactory; protected $fillable = [ 'name', 'is_completed', 'due_date', ]; protected function casts(): array { return [ 'is_completed' => 'boolean', 'due_date' => 'date' ]; }}
Validation rules:
app/Http/Requests/StoreTaskRequest.php:
// ...public function rules(): array{ return [ 'name' => ['required', 'string', 'max:255'], 'due_date' => ['nullable', 'date'], ];}
app/Http/Requests/UpdateTaskRequest.php:
public function rules(): array{ return [ 'name' => ['required', 'string', 'max:255'], 'is_completed' => ['required', 'boolean'], 'due_date' => ['nullable', 'date'], ];}
No modifications are needed for the Controller since we use $request->validated() to create/update tasks.
Ok, the back end is ready. It's time to dive into the JavaScript/TypeScript side.
Adding Field to TypeScript Types
We need to update our type in the file of all types:
resources/js/types/index.d.ts
export interface Task { id: number; name: string; is_completed: boolean; due_date?: string; created_at: string; updated_at: string;}
Also, in our Create.tsx page, we need to...
How hard would it be to set the calende picker with the date and time aswell ?
If we go for native browser, maybe input type="datetime-local"?
I had problems with the
date-fnslibrary, incorrect format in the picker, I useddayjsinstead.Can you show us what you changed to get this to work? I added the npm install dayjs –save But what else do I have to do to get this to work?
date-fns worked for me without any problems.
In Create.tsx
Use the
formatfunctionWhat problems were you having?
Hi Povilas,
Something minor but important: In Create.tsx, the form is defined in the article as;
But it was actually created earlier as type and should be:
The same with EditTaskForm:
Hi, thanks for letting us know! I've updated the lesson to fix this :)
ps. This was a left-over update from following starter kit changes https://github.com/laravel/react-starter-kit/commit/bb97832a09926ab9c28b6bde87262fca4bc1800a#diff-6dcdf3ad723e9f82263160236fdb941e99915e8171a4f807b8a95a5c2aa89841R13
Under "Adding the Input Date to the Form" (For Create) , the following is missing:
This would set the
due_dateto today, but we intentionally left itnullas we don't want to fill it with any default value.Or was there any other reason for this?
I meant the
due_dateproperty is missing which throws an errorBut it is there, no?
This is taken from the lesson and seems to be exactly what you are referring to
Ah! seen that it was previously set before that secton. No worries!
While the Edit form works with the addition of
due_date, TypeScript is complaining, "Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'." I believe this is because we have defineddue_dateas optional in the type but by wrappingEditTaskFormwithRequired, we are making all the properties ofEditTaskFormrequired.I was able to fix the TypeScript error by making
due_dateoptional as follows:I removed the Required from useForm and passed due_date or null
As of today, Shadcn date picker can now easily be used here.