Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Pre-filled fields in ModelAdmin


Go to End


3 Posts   1353 Views

Avatar
ec8or

Community Member, 18 Posts

6 November 2010 at 8:41am

Hi,

When adding a new entry using ModelAdmin I would like to have a dropdown list that fills out fields automatically on select.

I was thinking the best way to do this was to create a separate DataObject that holds all these "template" objects, and then use jQuery to loop through all the values on select and populate the relevant fields but not sure where to start, so if someone could point me in the right direction I would appreciate it. Ideally, I'd like to add a 'Save as default' button when creating a new record that would then create a new entry that would get added to the dropdown.

Cheers

Avatar
swaiba

Forum Moderator, 1899 Posts

6 November 2010 at 9:19pm

create your dataobject for defaults
using getCMSFields add you new dropdown to select the defaults record on the target dataobject
use the jQuery to add on change of the new dropdown the reads the new records and writes them into the form
create an ajax function for the onchange to call that passes the selection and returns a JSON array of the required values

alternatively you could just add the new dropdown and then put all of the work into onbeforewrite - this could check the value of this one field and then depending on it's value populate everything else before the write

Avatar
ec8or

Community Member, 18 Posts

9 November 2010 at 8:02am

The jQuery approach is for sure the most proper but onBeforeWrite sounds like a much quicker fix, didn't think of that so cheers!