Pre-fill the create form with some data

stephen

Sep 1st, 2017 06:30 PM

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

mark

Sep 2nd, 2017 08:41 AM

I think this feature could become a part of Voyager. Can I ask you go create a detailed issue on Github?

stephen

Sep 2nd, 2017 10:19 AM

That would be great if it could be integrated!

I've made a ticket here: https://github.com/the-control-group/voyager/issues/1649

PS: I tried to make it a link but the edit functionality seems a bit broken.

stephen

Sep 2nd, 2017 10:39 AM

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 :)

mark

Sep 2nd, 2017 10:42 AM

That looks like a good aproach. Then the system just needs to check for the query parameters. If you manage to do this, please send a pull request.

stephen

Sep 2nd, 2017 06:06 PM

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...)