Pre-fill the create form with some data
Hello!
In my application, I have some related tables that I would like to pre-populate certain fields via a link in a custom view.
For example, I have a table clients and while editing a client record, I need to be able to add tasks for the current client. There is already a client_id foreign key in the tasks table and I've set up a relationship but I'm wondering what is the best way to pass the client_id across in a link from the main client record view? The goal is to have the client_id pre-filled to save extra data entry.
I guess I will need to create a custom route and view for the tasks add form and pre-populate the data from the URL. It shouldn't be too complicated but since this seems like a fairly common scenario, I'm curious to know how others have done it...
Thanks!
Stephen
Thinking about this a bit more, I realise I can just add some querystring parameters onto the URL without having to touch the routes.
eg. /admin/tasks/create?client_id=123
Then I can read this in the view via Request::get('client_id')
I think that's the approach I'll take for now but I'm interested in any better solutions in the future :)
Originally I was thinking this would be just for custom views but it would be nice to be able to override the default views.
In order to do that, it would require changes to all the formfields/*.blade.php
views. I'll see what I can come up with...
I think the most logical approach is to check if a querystring variable with the same name as each field exists and use that as the value. The querystring value should probably take precedence over other values for the field (normally I would see this being used on create forms but maybe there are some use cases for overriding a value in an edit form...)